swift简单动画demo

from:
swift简单动画demo

动画GIF

No. name(cn) name(en) gif
1 缩放动画 Scale scale
2 位移动画 Translation scale
3 旋转动画 Rotation scale
4 颜色动画 Color scale
5 弹簧动画 Spring scale
6 组合动画 Concat scale

代码

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import UIKit

class ViewController: UIViewController {



var rect: UIView!

override func viewDidLoad() {
super.viewDidLoad()

initrectView()
}

var i: Int = 0

@IBAction func onNextButtonClick(sender: UIButton) {
switch i%6 {
case 0:
showScaleAnim() // 1. 缩放动画
case 1:
showTranslationAnim() // 2. 位移动画
case 2:
showRotationAnim() // 3. 旋转动画
case 3:
showColorAnim() // 4. 颜色动画
case 4:
showSpringAnim() // 5. 弹簧动画
default:
showConcatAnim() // 6. 组合动画
}

i += 1
}


/**
初始化动画对象
*/

func initrectView() {
let rectFrame = CGRectMake(50, 50, 100, 100)
rect = UIView(frame: rectFrame)

rect.backgroundColor = UIColor.cyanColor()

self.view.addSubview(rect)
}

func resumeRectView() -> Void {
rect.transform = CGAffineTransformIdentity
}

/**
普通动画. 缩放
*/

func showScaleAnim() {

let duration: NSTimeInterval = 3 // 动画持续时间
let delay: NSTimeInterval = 0 // 动画延迟时间
let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
let animations: () -> Void = {() -> Void in
self.rect.transform = CGAffineTransformMakeScale(2, 2) // 缩放动画, x 放大 2, y 放大 2
}
UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations) { (finish) -> Void in
self.resumeRectView()
print(finish)
}
}

/**
普通动画. 位移
*/

func showTranslationAnim() {
let duration: NSTimeInterval = 3 // 动画持续时间
let delay: NSTimeInterval = 0 // 动画延迟时间
let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
let animations: () -> Void = {() -> Void in
self.rect.transform = CGAffineTransformMakeTranslation(0, 100) // 位移动画, x 移动 0, y 移动 100
}
UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations) { (finish) -> Void in
self.resumeRectView()
print(finish)
}
}

/**
普通动画. 旋转
*/

func showRotationAnim() {
let duration: NSTimeInterval = 3 // 动画持续时间
let delay: NSTimeInterval = 0 // 动画延迟时间
let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
let animations: () -> Void = {() -> Void in
self.rect.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) // 旋转动画, 旋转pi
}
UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations) { (finish) -> Void in
self.resumeRectView()
print(finish)
}
}

/**
普通动画. 颜色
*/

func showColorAnim() {
let duration: NSTimeInterval = 3 // 动画持续时间
let delay: NSTimeInterval = 0 // 动画延迟时间
let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
let animations: () -> Void = {() -> Void in
self.rect.backgroundColor = UIColor.greenColor() // 颜色动画, 变色为绿色
}
UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations) { (finish) -> Void in
self.resumeRectView()
print(finish)
}
}

/**
弹簧动画, 一个球变大
*/

func showSpringAnim() {
let duration: NSTimeInterval = 3 // 动画持续时间
let delay: NSTimeInterval = 0 // 动画延迟时间
let damping: CGFloat = 0.3 // 阻尼. 越大感觉越有粘性
let velocity: CGFloat = 0 // 初速度
let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
let animations: () -> Void = {() -> Void in
self.rect.transform = CGAffineTransformMakeTranslation(0, 150) // 位移动画
}

UIView.animateWithDuration(duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: velocity, options: options, animations: animations) { (finish) -> Void in
self.resumeRectView()
print(finish)
}
}

/**
组合动画. 缩放 + 位移 + 颜色
*/

func showConcatAnim() {
let duration: NSTimeInterval = 3 // 动画持续时间
let delay: NSTimeInterval = 0 // 动画延迟时间
let options: UIViewAnimationOptions = UIViewAnimationOptions.CurveEaseInOut // 动画选项
let animations: () -> Void = {() -> Void in

let anim1 = CGAffineTransformMakeScale(2, 2) // 缩放动画, x 放大 2, y 放大 2
let anim2 = CGAffineTransformMakeTranslation(150, 50) // 位移动画, x 移动 150, y 移动 50

let concatAnim = CGAffineTransformConcat(anim1, anim2) // 组合动画, 缩放和位移动画一起组合成新的动画

self.rect.backgroundColor = UIColor.greenColor() // 颜色动画

self.rect.transform = concatAnim
}
UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations) { (finish) -> Void in
self.resumeRectView()
print(finish)
}
}
}
Fork me on GitHub