IOS动画效果主要有两种方法:
一种是UIView层面的,
一种是使用CATransition进行更低层次的控制,
1.使用UIView类函数实现:
// UIViewAnimationTransitionFlipFromLeft, 向左转动 // UIViewAnimationTransitionFlipFromRight, 向右转动 // UIViewAnimationTransitionCurlUp, 向上翻动 // UIViewAnimationTransitionCurlDown, 向下翻动 [UIView beginAnimations:@"animationID" context:nil]; [UIView setAnimationDuration:0.5f]; //动画时长 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; //给视图添加过渡效果 //在这里写你的代码. [UIView commitAnimations]; //提交动画
2.使用CATransition对象来实现:
CATransition比较强大,一般可以使用CATransition模拟UIView的动画。
| Transition | Subtypes | Accepted parameters |
|---|---|---|
|
moveIn push reveal |
fromLeft, fromRight, fromBottom, fromTop | - |
| pageCurl, pageUnCurl | fromLeft, fromRight, fromTop, fromBottom | float inputColor[]; |
|
cube alignedCube |
fromLeft, fromRight, fromTop, fromBottom | float inputAmount; (perspective) |
|
flip alignedFlip oglFlip |
fromLeft, fromRight, fromTop, fromBottom | float inputAmount; |
| cameraIris | - | CGPoint inputPosition; |
| rippleEffect | - | - |
| rotate | 90cw, 90ccw, 180cw, 180ccw | - |
| suckEffect | - | CGPoint inputPosition; |
[代码]
CATransition *animation = [CATransition animation]; animation.delegate = self; animation.duration = 0.5f; //动画时长 animation.timingFunction = UIViewAnimationCurveEaseInOut; animation.fillMode = kCAFillModeForwards; animation.type = @”cube”; //过度效果 animation.subtype = @”formLeft”; //过渡方向 animation.startProgress = 0.0 //动画开始起点(在整体动画的百分比) animation.endProgress = 1.0; //动画停止终点(在整体动画的百分比) animation.removedOnCompletion = NO; [self.view.layer addAnimation:animation forKey:@"animation"];
1 + (void)showAnimationType:(NSString *)type 2 withSubType:(NSString *)subType 3 duration:(CFTimeInterval)duration 4 timingFunction:(NSString *)timingFunction 5 view:(UIView *)theView 6 { 7 /** CATransition 8 * 9 * @see http://www.dreamingwish.com/dream-2012/the-concept-of-coreanimation-programming-guide.html 10 * @see http://geeklu.com/2012/09/animation-in-ios/ 11 * 12 * CATransition 常用设置及属性注解如下: 13 */ 14 15 CATransition *animation = [CATransition animation]; 16 17 /** delegate 18 * 19 * 动画的代理,如果你想在动画开始和结束的时候做一些事,可以设置此属性,它会自动回调两个代理方法. 20 * 21 * @see CAAnimationDelegate (按下command键点击) 22 */ 23 24 animation.delegate = self; 25 26 /** duration 27 * 28 * 动画持续时间 29 */ 30 31 animation.duration = duration; 32 33 /** timingFunction 34 * 35 * 用于变化起点和终点之间的插值计算,形象点说它决定了动画运行的节奏,比如是均匀变化(相同时间变化量相同)还是 36 * 先快后慢,先慢后快还是先慢再快再慢. 37 * 38 * 动画的开始与结束的快慢,有五个预置分别为(下同): 39 * kCAMediaTimingFunctionLinear 线性,即匀速 40 * kCAMediaTimingFunctionEaseIn 先慢后快 41 * kCAMediaTimingFunctionEaseOut 先快后慢 42 * kCAMediaTimingFunctionEaseInEaseOut 先慢后快再慢 43 * kCAMediaTimingFunctionDefault 实际效果是动画中间比较快. 44 */ 45 46 /** timingFunction 47 * 48 * 当上面的预置不能满足你的需求的时候,你可以使用下面的两个方法来自定义你的timingFunction 49 * 具体参见下面的URL 50 * 51 * @see http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CAMediaTimingFunction_class/Introduction/Introduction.html 52 * 53 * + (id)functionWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y; 54 * 55 * - (id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y; 56 */ 57 58 animation.timingFunction = [CAMediaTimingFunction functionWithName:timingFunction]; 59 60 /** fillMode 61 * 62 * 决定当前对象过了非active时间段的行为,比如动画开始之前,动画结束之后. 63 * 预置为: 64 * kCAFillModeRemoved 默认,当动画开始前和动画结束后,动画对layer都没有影响,动画结束后,layer会恢复到之前的状态 65 * kCAFillModeForwards 当动画结束后,layer会一直保持着动画最后的状态 66 * kCAFillModeBackwards 和kCAFillModeForwards相对,具体参考上面的URL 67 * kCAFillModeBoth kCAFillModeForwards和kCAFillModeBackwards在一起的效果 68 */ 69 70 animation.fillMode = kCAFillModeForwards; 71 72 /** removedOnCompletion 73 * 74 * 这个属性默认为YES.一般情况下,不需要设置这个属性. 75 * 76 * 但如果是CAAnimation动画,并且需要设置 fillMode 属性,那么需要将 removedOnCompletion 设置为NO,否则 77 * fillMode无效 78 */ 79 80 // animation.removedOnCompletion = NO; 81 82 /** type 83 * 84 * 各种动画效果 其中除了\'fade\', `moveIn\', `push\' , `reveal\' ,其他属于似有的API(我是这么认为的,可以点进去看下注释). 85 * ↑↑↑上面四个可以分别使用\'kCATransitionFade\', \'kCATransitionMoveIn\', \'kCATransitionPush\', \'kCATransitionReveal\'来调用. 86 * @"cube" 立方体翻滚效果 87 * @"moveIn" 新视图移到旧视图上面 88 * @"reveal" 显露效果(将旧视图移开,显示下面的新视图) 89 * @"fade" 交叉淡化过渡(不支持过渡方向) (默认为此效果) 90 * @"pageCurl" 向上翻一页 91 * @"pageUnCurl" 向下翻一页 92 * @"suckEffect" 收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向) 93 * @"rippleEffect" 滴水效果,(不支持过渡方向) 94 * @"oglFlip" 上下左右翻转效果 95 * @"rotate" 旋转效果 96 * @"push" 97 * @"cameraIrisHollowOpen" 相机镜头打开效果(不支持过渡方向) 98 * @"cameraIrisHollowClose" 相机镜头关上效果(不支持过渡方向) 99 */ 100 101 /** type 102 * 103 * kCATransitionFade 交叉淡化过渡 104 * kCATransitionMoveIn 新视图移到旧视图上面 105 * kCATransitionPush 新视图把旧视图推出去 106 * kCATransitionReveal 将旧视图移开,显示下面的新视图 107 */ 108 109 animation.type = type; 110 111 /** subtype 112 * 113 * 各种动画方向 114 * 115 * kCATransitionFromRight; 同字面意思(下同) 116 * kCATransitionFromLeft; 117 * kCATransitionFromTop; 118 * kCATransitionFromBottom; 119 */ 120 121 /** subtype 122 * 123 * 当type为@"rotate"(旋转)的时候,它也有几个对应的subtype,分别为: 124 * 90cw 逆时针旋转90° 125 * 90ccw 顺时针旋转90° 126 * 180cw 逆时针旋转180° 127 * 180ccw 顺时针旋转180° 128 */ 129 130 /** 131 * type与subtype的对应关系(必看),如果对应错误,动画不会显现. 132 * 133 * @see http://iphonedevwiki.net/index.php/CATransition 134 */ 135 136 animation.subtype = subType; 137 138 /** 139 * 所有核心动画和特效都是基于CAAnimation,而CAAnimation是作用于CALayer的.所以把动画添加到layer上. 140 * forKey 可以是任意字符串. 141 */ 142 143 [theView.layer addAnimation:animation forKey:nil]; 144 } 145
1 /** CABasicAnimation 2 * 3 * @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html 4 * 5 * @brief 便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个 6 * 键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果 7 * 具体可以填写什么请参考上面的URL,切勿乱填! 8 * 例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度. 9 * 这个动画的效果是把view旋转到最小,再旋转回来. 10 * 你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类. 11 * 12 * @param toValue 动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为: 13 * fromValue(开始值), toValue(结束值), byValue(偏移值), 14 ! 这三个属性最多只能同时设置两个; 15 * 他们之间的关系如下: 16 * 如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue; 17 * 如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue; 18 * 如果同时设置了byValue 和toValue,那么动画就会从toValue - byValue过渡到toValue; 19 * 20 * 如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value; 21 * 如果只设置了toValue ,那么动画就会从当前的value过渡到toValue; 22 * 如果只设置了byValue ,那么动画就会从从当前的value过渡到当前value + byValue. 23 * 24 * 可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并 25 * 同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值. 26 ! 而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去 27 * 完成这些显示效果而已. 28 * 在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度. 29 * 30 * @param duration 动画持续时间 31 * 32 * @param timingFunction 动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢... 33 */ 34 35 /** CAAnimationGroup 36 * 37 * @brief 顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画, 38 * 把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性, 39 * 这时候就可以使用CAAnimationGroup. 40 * 41 * @param duration 动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一 42 * 设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性 43 * 的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画. 44 * 45 * @param autoreverses 动画完成后自动重新开始,默认为NO. 46 * 47 * @param repeatCount 动画重复次数,默认为0. 48 * 49 * @param animations 动画组(数组类型),把需要同时运行的动画加到这个数组里. 50 * 51 * @note addAnimation:forKey 这个方法的forKey参数是一个字符串,这个字符串可以随意设置. 52 * 53 * @note 如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把 54 * removedOnCompletion 设置为NO; 55 */ 56 57 + (void)animationRotateAndScaleDownUp:(UIView *)view 58 { 59 CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 60 rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2]; 61 rotationAnimation.duration = 0.35f; 62 rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 63 64 CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 65 scaleAnimation.toValue = [NSNumber numberWithFloat:0.0]; 66 scaleAnimation.duration = 0.35f; 67 scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 68 69 CAAnimationGroup *animationGroup = [CAAnimationGroup animation]; 70 animationGroup.duration = 0.35f; 71 animationGroup.autoreverses = YES; 72 animationGroup.repeatCount = 1; 73 animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil]; 74 [view.layer addAnimation:animationGroup forKey:@"animationGroup"]; 75 } 76
[引用]http://www.cnblogs.com/pengyingh/articles/2339420.html