模态(Modal)界面弹出动画一般是从下往上推出,如果要修改转场动画,可以设置modalTransitionStyle属性,代码如下:

ViewController *vc = [[ViewController alloc] init];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:nil];

modalTransitionStyle是一个枚举,有以下四个值,分别代表四种不同的转场效果,

UIModalTransitionStyleCoverVertical = 0,
UIModalTransitionStyleFlipHorizontal,
UIModalTransitionStyleCrossDissolve,
UIModalTransitionStylePartialCurl

如果上面四种效果都不能满足你的需求,还可以使用CATransition进一步定制你的转场动画,代码如下:

ViewController *vc = [[ViewController alloc] init];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromLeft;
transition.duration = 3.0;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.view.window.layer addAnimation:transition forKey:@"tran"];
[self presentViewController:vc animated:NO completion:nil];

使用CATransition的type和subtype两个属性可以定制16中转场动画,同时CATransition还可以设置转场持续的时间(duration)以及动画速度控制函数(timingFunction)等属性。注意,一定要把动画加载控制器view的window上。

完成代码,包括present和dismiss

ViewController *vc = [[ViewController alloc] init];
CATransition *presentTransition = [CATransition animation];
presentTransition.type = kCATransitionMoveIn;
presentTransition.subtype = kCATransitionFromLeft;
presentTransition.duration = 3.0;
presentTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.view.window.layer addAnimation:presentTransition forKey:@"presentTransition"];
[self presentViewController:vc animated:NO completion:nil];


CATransition *dismissTransition = [CATransition animation];
dismissTransition.type = kCATransitionMoveIn;
dismissTransition.subtype = kCATransitionFromRight;
dismissTransition.duration = 3.0;
dismissTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.view.window.layer addAnimation:dismissTransition forKey:@"dismissTransition"];
[self dismissViewControllerAnimated:NO completion:nil];

如果CATransition的16种转场动画还不能满足你的需求,iOS7之后,可以使用UIViewControllerAnimatedTransitioning协议完全自定义转场动画,还能实现手势控制转动画。具体代码可以参考我写的这个Demo

以上就是我对模态界面转场动画的一个总结。