在iPhone开发中,我们常常需要在不同的页面之间做动画切换,这样看起来更加好看。基本动画有以下三种:
1、UIView
UIView官方提供五种动画效果供大家使用,分别为:
nNone; 不使用动画
nFlipFromLeft;从左向右旋转翻页
nFlipFromRight;从右向左旋转翻页
nCurlUp; 卷曲翻页,从下往上
nCurlDown; 卷曲翻页,从上往下
例子:
[UIView beginAnimations:@"animationID" context:context];//开始一个动画块,第一个参数为动画块标识
InOut];//设置动画块中的动画属性变化的曲线,此方法必须在beginAnimations和commitAnimations方法中。
[UIView setAnimationDuration:kDuration];//设置动画的持续时间
verses:NO];//设置是否自动反转当前的动画效果
[UIView setAnimationTransition:transition forView:self.view cache:YES];//设置过渡的动画效果,此处第一个参数可使用上面5种动画效果
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];//页面翻转
[UIView commitAnimations];//提交动画
2、CATransition
CATransition官方提供4种动画效果,分别为:
animation.type = kCATransitionFade;//渐渐消失
animation.type = kCATransitionPush;//推出
animation.type = kCATransitionReveal;//与MoveIn相反
animation.type = kCATransitionMoveIn;//覆盖进入
例子:
CATransition *animation = [CATransition animation];//初始化动画
animation.duration = kDuration;//间隔的时间
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionFade;//渐渐消失 设置动画效果
//设置动画的方向,有四种,分别为:
animation.subtype = kCATransitionFromLeft;
animation.subtype = kCATransitionFromBottom;
animation.subtype = kCATransitionFromRight;
animation.subtype = kCATransitionFromTop;
[[self.view layer] addAnimation:animation forKey:@"animation"];
//注意subtype的动画方向是根据self.view的frame的坐标来确定的,要注意此时self.view的uiinterfaceorientation的方向
//比如说此时viewcontroller是rootviewcontroller或被presentViewController显示的,且此时view.interfaceorientation是uiinterfaceorientationlandscape,此时设备是横屏显示的,假如对self.view中的两个subview进行动画,且设置subtype为kCATransitionFromLeft/kCATransitionFromRight,那么此时在视觉效果上,图片是向上或向下进行动画的
3、私有动画
私有动画是在CATransition的基础上,设置animation.type,可以提供一下几种选择:
animation.type = @"cube";//像立方体那样翻转
animation.type = @"suckEffect";//吸入,渐渐缩小,与删除照片的动画一样
nFlipFromLeft;和
nFlipFromRight一样
animation.type = @"rippleEffect";//水波效果
nCurlUp一样
nCurlDown一样
animation.type = @"cameraIrisHollowOpen";//摄像头开
animation.type = @"cameraIrisHollowClose";//摄像头关