1 import UIKit
2
3 class ViewController:UIViewController {
4
5 override func viewDidLoad() {
6 super.viewDidLoad()
7 // Do any additional setup after loading the view,
typically from a nib.
8 let rect = CGRect(x:40, y:80, width:240, height:
9 let view = UIView(frame:rect)
10 view.backgroundColor = UIColor.red
11 view.tag = 1
12 self.view.addSubview(view)
13
14 let button = UIButton(type:UIButtonType.System)
15 button.frame = CGRect(x:50, y:400, width:220,
height:44)
16 button.backgroundColor = UIColor.black
17 button.setTitle(“Play”, for:UIControlState())
18 button.addTarget(self, action:
selector(ViewController.playAnimation), for:
UIControlEvents.touchUpInside)
19 self.view.addSubview(button)
20 }
21
22 func playAnimation()
23 {
24 UIView.beginAnimations(nil, context:nil)
25 UIView.setAnimationCurve(.easeOut)
26 UIView.setAnimationDuration(5)
27 UIView.setAnimationBeginsFromCurrentState(true)
28
29 let view = self.view.viewWithTag(1)
30 view?.frame = CGRect(x:40, y:40, width:0,
height:0)
31 view?.backgroundColor = UIColor.blue
32 view?.alpha = 0
33
34 UIView.setAnimationDelegate(self)
35
UIView.setAnimationDidStop(#selector(ViewController.animationStop))
36 UIView.commitAnimations()
37 }
38
39 func animationStop()
40 {
41 print(“Animaton stop.”)
42 self.view.viewWithTag(1)?.removeFromSuperview()
43 }
44
45 override func didReceiveMemoryWarning() {
46 super.didReceiveMemoryWarning()
47 // Dispose of any resources that can be recreated.
48 }
49 }
//动画类型:速度类型
image.png