CoreAnimation實(shí)戰(zhàn)
CABasicAnimation
// 創(chuàng)建動(dòng)畫
let anim = CABasicAnimation()
// 描述下修改哪個(gè)屬性產(chǎn)生動(dòng)畫
// 只能是layer屬性
anim.keyPath = "transform.scale";
//設(shè)置值
anim.toValue = NSValue(CGPoint: CGPointMake(250, 500))
anim.toValue = 0.5;
// 設(shè)置動(dòng)畫執(zhí)行次數(shù)
anim.repeatCount = MAXFLOAT;
// 取消動(dòng)畫反彈
//設(shè)置動(dòng)畫完成的時(shí)候不要移除動(dòng)畫
anim.removedOnCompletion = false;
//設(shè)置動(dòng)畫執(zhí)行完成要保持最新的效果
anim.fillMode = kCAFillModeForwards;
imageV.layer.addAnimation(anim, forKey: nil)
CAKeyframeAnimation
CAKeyframeAnimation
本質(zhì)上是CABasicAnimation
的特殊化胆建,只是變換的值多了,用values
代表多個(gè)數(shù)值豫柬。其中path
屬性可以讓你的圖層按照你制定的路線移動(dòng).
CATransition
CATransition
分為單視圖和多視圖之間的切換陨闹,其中視圖之間的切換需要實(shí)現(xiàn)一個(gè)UIViewControllerAnimatedTransitioning
協(xié)議。
單視圖的轉(zhuǎn)場(chǎng)動(dòng)畫
單視圖是對(duì)一個(gè)視圖添加動(dòng)畫而不是轉(zhuǎn)場(chǎng)的動(dòng)畫聘芜。
// 轉(zhuǎn)場(chǎng)動(dòng)畫
let anim = [CATransition()
anim.type = "rippleEffect"
anim.duration = 2;
imageV.layer.addAnimation(anim, forKey: nil)
轉(zhuǎn)場(chǎng)動(dòng)畫
使用UINavigationController
進(jìn)行轉(zhuǎn)場(chǎng)的時(shí)候兄渺,需要重載 UINavigationControllerDelegate
的navigationController:operation :fromVC:toVC:
方法;使用UIViewController
的presentViewController
時(shí)重載UIViewControllerTransitioningDelegate
的方法汰现。兩者返回的都是一個(gè)
UIViewControllerAnimatedTransitioning
對(duì)象挂谍。具體步驟如下
- 創(chuàng)建繼承自
NSObject
并且聲明UIViewControllerAnimatedTransitioning
協(xié)議的動(dòng)畫類叔壤。
UIViewControllerAnimatedTransitioning
是一個(gè)動(dòng)畫制作器協(xié)議,需要和控制動(dòng)畫時(shí)間(timing)的對(duì)象綁定.animateTransition:
里實(shí)現(xiàn)的方法就是動(dòng)畫本身(重寫這個(gè)方法)
-
animateTransition
方法中繪制動(dòng)畫過程:
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//獲取兩個(gè)VC
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
//確定最后的Frame大小
let finalFrameForVC = transitionContext.finalFrameForViewController(toViewController)
//獲取動(dòng)畫展現(xiàn)的view口叙,containerView炼绘,動(dòng)畫就是在這個(gè)view上進(jìn)行的
let containerView = transitionContext.containerView()
//動(dòng)畫具體過程
//....
//...
- 在
UIViewController
中實(shí)現(xiàn)UIViewControllerTransitioningDelegate
協(xié)議的
animationControllerForPresentedController(_:presentingController:sourceController:)
返回一個(gè)自定義transiton對(duì)象 - 實(shí)現(xiàn)
UIViewControl ![屏幕快照 2016-06-26 02.23.38.png](http://upload-images.jianshu.io/upload_images/701672-e7d70b98bd6b5e36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) lerTransitioningDelegate
協(xié)議的animationControllerForDismissedController
返回一個(gè)自定義的dismiss的transiton對(duì)象 - 對(duì)于
UINavigationControllerDelegate
則要根據(jù)這個(gè)push和pop來區(qū)分出棧和入棧的操作。在你的自定義轉(zhuǎn)場(chǎng)動(dòng)畫中需要針對(duì)push和pop制定兩種效果navigationController(\_:animationControllerForOperation:fromViewController:toViewController:)
中實(shí)現(xiàn)
CAAnimationGroup
讓一個(gè)圖層實(shí)現(xiàn)多個(gè)動(dòng)畫效果妄田,用法和上面相似俺亮,只是把所有的動(dòng)畫加到一起,然后添加到圖層上去
let group = CAAnimationGroup()
let scale = CABasicAnimation()
scale.keyPath = "transform.scale"
scale.toValue = 0.5
let rotation = CABasicAnimation()
rotation.keyPath = "transform.rotation";
rotation.toValue = (arc4random_uniform(M_PI));
group.animations = [scale,rotation];
View.layer addAnimation(group forKey:nil)