iOS 抖动动画

Posted by He Zongjiang on 2018-09-28
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
/// 抖动方向枚举
public enum ShakeDirection {
/// 水平抖动
case horizontal
/// 垂直抖动
case vertical
}

extension UIView {
/// 扩展UIView增加抖动方法
///
/// - Parameters:
/// - direction: 抖动方向(默认是水平方向)
/// - times: 抖动次数(默认6次)
/// - interval: 每次抖动时间(默认0.1秒)
/// - delta: 抖动偏移量(默认4)
/// - completion: 抖动动画结束后的回调
public func shake(direction: ShakeDirection = .horizontal, times: Int = 6, interval: TimeInterval = 0.1, delta: CGFloat = 4, completion: ((Bool) -> ())? = nil) {

UIView.animate(withDuration: interval, animations: {
switch direction {
case .horizontal:
self.layer.setAffineTransform( CGAffineTransform(translationX: delta, y: 0))
case .vertical:
self.layer.setAffineTransform( CGAffineTransform(translationX: 0, y: delta))
}

}) { (_) in

if (times == 0) { // 最后一次抖动,将位置还原,并调用完成回调函数
UIView.animate(withDuration: interval, animations: {
self.layer.setAffineTransform(CGAffineTransform.identity)
}, completion: completion)
} else { // 不是最后一次抖动,继续播放动画(总次数减1,偏移位置反向)
self.shake(direction: direction, times: times - 1, interval: interval, delta: delta * -1, completion:completion)
}
}
}
}