运用Core Animaiton进行UIViewController之间的切换

在iOS中,我们处理动画,主要用到两种方式,

一:运用UIView 中的一系列的animation方法

二:运用Core Animation进行操作

今天就来说说第二种方式的简单运用,结合我运用中出现的问题,

下面模拟从AViewController切换到BViewController.

直接上代码:

AViewController.m
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
28
29
30
31
32
33
34
35
36
37
@implementation AViewController {
BViewController *bViewController;
}

- (void)viewDidLoad
{
[super viewDidLoad];

bViewController = [[BViewController alloc] init];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"push" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pushBtnClick) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(50, 50, 50, 20);
[self.view addSubview:button];

self.view.tag = 10000;

self.view.backgroundColor = [UIColor grayColor];
}

- (void)pushBtnClick {
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromRight;

bViewController.view = NO;
[self.view.superview insertSubview:bViewController.view belowSubview:self.view];
NSUInteger first = [self.view.superview.subviews indexOfObject:bViewController.view];
NSUInteger second = [self.view.superview.subviews indexOfObject:self.view];
[self.view.superview exchangeSubviewAtIndex:first withSubviewAtIndex:second];
[[self.view.superview layer] addAnimation:animation forKey:@"partyCell"];
self.view.hidden = YES;
}
BViewController.m
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
28
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"back" forState:UIControlStateNormal];
[button addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 50, 20);
[self.view addSubview:button];
self.view.backgroundColor = [UIColor redColor];
}

- (void)backBtnClick {
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromLeft;

[self.view.superview viewWithTag:10000].hidden = NO;
NSUInteger first = [self.view.superview.subviews indexOfObject:self.view];
NSUInteger second = [self.view.superview.subviews indexOfObject:[self.view.superview viewWithTag:10000]];
[self.view.superview exchangeSubviewAtIndex:first withSubviewAtIndex:second];
[[self.view.superview layer] addAnimation:animation forKey:@"partyCell"];
self.view.hidden = YES;
}

完整实例程序:下载链接

文章目录
,