Swift - 使用CAKeyframeAnimation实现关键帧动画

 

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/6124633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值