1,CAKeyframeAnimation介绍
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.0, 100.0) let p2 = CGPointMake(300, 100.0) let p3 = CGPointMake(100.0, 400) let p4 = CGPointMake(300, 400) let p5 = CGPointMake(150, 200) //赋值 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(100, 100, 100, 100)) 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 ( "动画结束" )
} |