Swift

 

1,CAKeyframeAnimation介绍

CAKeyframeAnimation可以实现关键帧动画,这个类可以实现某一属性按照一串的数值进行动画,就像是一帧一帧的制作出来一样。
 
2,使用样例(设置五个关键点坐标,图片依次按关键点移动)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

//

//  ViewController.swift

//  CAKeyframeAnimation

//

//  Created by Wengrp on 16/12/1.

//  Copyright © 2016 wengrenpu. All rights reserved.

//

 

import UIKit

 

class ViewController: UIViewController {

    

    var imageView: UIView!

 

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let animation = CAKeyframeAnimation(keyPath: "position")

        

        //设置5个位置点

        let p1 = CGPointMake(100.0100.0)

        let p2 = CGPointMake(300100.0)

        let p3 = CGPointMake(100.0400)

        let p4 = CGPointMake(300400)

        let p5 = CGPointMake(150200)

        

        //赋值

        animation.values = [NSValue(CGPoint: p1), NSValue(CGPoint: p2),

                            NSValue(CGPoint: p3), NSValue(CGPoint: p4), NSValue(CGPoint: p5)]

        

        //每个动作的时间百分比

        animation.keyTimes = [NSNumber(float: 0.0), NSNumber(float: 0.4),

                              NSNumber(float: 0.6), NSNumber(float: 0.8), NSNumber(float: 1.0), ]

        

        animation.delegate = self

        animation.duration = 6.0

        

        imageView = UIView(frame: CGRectMake(100100100100))

        imageView.backgroundColor = UIColor.cyanColor()

        imageView.layer.addAnimation(animation, forKey: "Image-Move")

        self.view.addSubview(imageView)

    }

    

    override func animationDidStart(anim: CAAnimation) {

        print("动画开始")

    }

    

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {

        print("动画结束")

    }

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

3,可以设置动画代理,监听开始和结束动作

1
2
3
4
5
6
7
8
9
animation.delegate = self
 
override func animationDidStart(anim: CAAnimation!) {
    println("动画开始")
}
 
override func animationDidStop(anim: CAAnimation!, finished flag: Bool) {
    println("动画结束")
}

PS:苹果官网API - CAKeyframeAnimation

原文地址:https://www.cnblogs.com/gongyuhonglou/p/10311560.html