/
NoEscapeAnimation.swift
51 lines (41 loc) · 1.34 KB
/
NoEscapeAnimation.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
//
// NoEscapeAnimation.swift
// NoEscapeAnimation
//
// Created by Alex Pretzlav on 3/1/15.
// Copyright (c) 2015 Alex Pretzlav. All rights reserved.
//
import UIKit
import Swift
private var delegates = Set<AnimationDelegate>()
public func animateViews(
duration duration: NSTimeInterval,
delay: NSTimeInterval = 0,
curve: UIViewAnimationCurve? = nil,
@noescape animations: () -> Void,
completion: (Bool -> Void)? = nil) {
UIView.beginAnimations(nil, context: nil)
if let completion = completion {
let wrapper = AnimationDelegate(callback: completion)
delegates.insert(wrapper)
UIView.setAnimationDelegate(wrapper)
UIView.setAnimationDidStopSelector("animationDidStop:finished:context:")
}
UIView.setAnimationDuration(duration)
UIView.setAnimationDelay(delay)
if let curve = curve {
UIView.setAnimationCurve(curve)
}
animations()
UIView.commitAnimations()
}
final class AnimationDelegate: NSObject {
let callback: Bool -> Void
init(callback: Bool -> Void) {
self.callback = callback
}
func animationDidStop(animationId: String?, finished: NSNumber, context: UnsafeMutablePointer<Void>) {
self.callback(finished.boolValue)
delegates.remove(self)
}
}