《精通 iOS 开发》第 06 章学习笔记

延迟加载处理

代码示例:

1
2
3
4
5
6
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.blueViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}

内存不足处理

代码示例:

1
2
3
4
5
6
7
8
9
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
if (!self.blueViewController.view.superview) {
self.blueViewController = nil;
} else {
self.yellowViewController = nil;
}
}

转换过程的动画效果

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- (IBAction)switchViews:(id)sender {
[UIView beginAnimations:@"View Flip" context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

if (!self.yellowViewController.view.superview) {
if (!self.yellowViewController) {
self.yellowViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Yellow"];
}

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];

[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
if (!self.blueViewController) {
self.blueViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
}

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];

[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}

[UIView commitAnimations];
}