-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+Animate.swift
53 lines (43 loc) · 1.49 KB
/
UIView+Animate.swift
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
//
// UIView+Animate.swift
// AnimationSequencer
//
// Created by Fabio Nisci on 10/03/2020.
//
import UIKit
public extension UIView {
func animate(_ animations:ASSequence...) {
self.animate(animations)
}
func animate(_ animations: [ASSequence], _ completion: (() -> Void)? = nil) {
guard !animations.isEmpty else { //faster than count
completion?()
return
}
var animations = animations
let animation = animations.removeFirst()
UIView.animate(withDuration: animation.duration, delay: animation.delay, options: animation.options, animations: {
animation.animation(self)
}, completion: { _ in
self.animate(animations, completion)
})
}
func animateTogether(_ animations: [ASSequence], _ completion: (() -> Void)? = nil) {
guard !animations.isEmpty else { //faster than count
completion?()
return;
}
let animationCompletionGroup = DispatchGroup()
animationCompletionGroup.notify(queue: DispatchQueue.main) {
completion?() //all animations are over
}
for animation in animations {
animationCompletionGroup.enter()
UIView.animate(withDuration: animation.duration, animations: {
animation.animation(self)
}, completion: { _ in
animationCompletionGroup.leave()
})
}
}
}