实现ios动画,主要有两种方法:UIView层面上的、使用CATransition。
一、UIView层面上的
[UIView beginAnimations:@"animation" context:nil]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; //交换本视图控制器中2个view位置 [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:1]; [UIView setAnimationDidStopSelector:@selector(animationFinish:)]; [UIView commitAnimations];
以beginAnimations:开始,以commitAnimations结束,关于动画的一切设置都写在这两个方法之间,不然无效。
二、使用CATransition
CATransition *transition = [CATransition animation]; transition.duration = 2.0f; transition.type = @"cube"; transition.subtype = kCATransitionFromLeft; [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; [self.view.layer addAnimation:transition forKey:@"animation"];
这里有两个属性要注意,type,Subtype,一般都是把这两个属性组合使用,我们来看一下官方对这两个属性的解释。
1、@property(copy) NSString *type;
type是这个transition的名字,当前合法的transition类型包括:`fade', `moveIn', `push' and `reveal',默认是`fade'。
它公开的类型可以有:淡化、移入、推挤、覆盖
NSString * const kCATransitionFade; NSString * const kCATransitionMoveIn; NSString * const kCATransitionPush; NSString * const kCATransitionReveal;
2、@property(copy) NSString *subtype;
subtype是这个transition可选的类型,比如,用来区分这个transition的方向,合法的值有`fromLeft', `fromRight', `fromTop' and * `fromBottom'.
它公开的类型可以有:
NSString * const kCATransitionFromRight; NSString * const kCATransitionFromLeft; NSString * const kCATransitionFromTop; NSString * const kCATransitionFromBottom;
3、本来没什么疑问,看到这个说明后,反而觉得奇怪,如果type只有这么几个类型,那cube、page等都是在哪实现的了?
cube等效果都不是它公开的方法,而是它提供的私有类型。
animation.type = @"cube" ; //立方体翻转 animation.type = @"suckEffect"; //收缩效果,如一块布被抽走 animation.type = @"oglFlip";//上下翻转效果,不管subType is "fromLeft" or "fromRight",official只有一种效果 animation.type = @"rippleEffect"; //滴水效果 animation.type = @"pageCurl"; //向上翻一页 animation.type = @"pageUnCurl" //向下翻一页 animation.type = @"cameraIrisHollowOpen "; //类似于ios的相机打开时的效果 animation.type = @"cameraIrisHollowClose "; //类似于ios的相机关闭时的效果
这些私有类型尽量少用。
4、CATransition的startProgress endProgress属性
这两个属性用来控制动画开始的点和结束的点,值在0.0--0.1之间。这个控制开始和结束的点,意思就是让动画不是从起点开始,而是从某一点开始,到某一点就停止动画效果,不是画面停止,而是动画效果停止。感觉没说清楚,这个自己试一下就知道了。
我们将在下一节详细介绍ios动画的各个属性设置。