1 override func viewDidLoad() {
2 super.viewDidLoad()
3 // Do any additional setup after loading the view,
typically from a nib.
4
5 let imageView = UIImageView(frame:CGRect(x:40,
y:80, width:64, height:64))
6 imageView.image = UIImage(named:“star”)
7 self.view.addSubview(imageView)
8
9 let animation =
CAKeyframeAnimation(keyPath:“position”)
10
11 let point1 = CGPoint(x:40, y:80)
12 let point2 = CGPoint(x:280, y:80)
13 let point3 = CGPoint(x:60, y:300)
14 let point4 = CGPoint(x:280, y:300)
15
16 animation.values = [NSValue(cgPoint:point1),
NSValue(cgPoint:point2),
17 NSValue(cgPoint:point3), NSValue(cgPoint:point4)]
18 animation.keyTimes = [NSNumber(value:0.0),
NSNumber(value:0.4),
19 NSNumber(value:0.6), NSNumber(value:1.0)]
20
21 animation.delegate = self
22 animation.duration = 5.0
23
24 imageView.layer.add(animation, forKey:“Move”)
25 }
26 func animationDidStart(_ anim:CAAnimation) {
27 print(“The animation starts”)
28 }
29
30 func animationDidStop(_ anim:CAAnimation,
finished flag:Bool) {
31 print(“End of the animation”)
32 }