Swift 4
advanced animation method () for watchOS :
- Duplicate
- Reversible
- Stoppable
- with completion block
import Foundation
import WatchKit
protocol Animatable: class {
func animate(forKey key: String,duration: TimeInterval, repeatCount count: CGFloat, autoreverses: Bool, animations: @escaping () -> Void, reverseAnimations: (() -> Void)?, completion: (() -> Void)?)
func stopAnimations()
}
extension Animatable {
func animate(forKey key: String, duration: TimeInterval, repeatCount count: CGFloat, autoreverses: Bool, animations: @escaping () -> Void, reverseAnimations: (() -> Void)?, completion: (() -> Void)?) {}
func stopAnimations() {}
}
extension WKInterfaceController: Animatable {
private struct AssociatedKeys {
static var animsDesc = "_animsDesc"
static var stopDesc = "_stopDesc"
}
var animations: [String: (() -> Void)?] {
get {return objc_getAssociatedObject(self, &AssociatedKeys.animsDesc) as? [String: (() -> Void)?] ?? [:]}
set {objc_setAssociatedObject(self, &AssociatedKeys.animsDesc, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)}
}
var animationStopStates: [String: Bool] {
get {return objc_getAssociatedObject(self, &AssociatedKeys.stopDesc) as? [String: Bool] ?? [:]}
set {objc_setAssociatedObject(self, &AssociatedKeys.stopDesc, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)}
}
func animate(forKey key: String, duration: TimeInterval, repeatCount count: CGFloat = 1.0, autoreverses: Bool = false, animations: @escaping () -> Void, reverseAnimations: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
let isStopped = self.animationStopStates[key] ?? false
if isStopped {
completion?()
return
}
self.setAnimations(key, reverse: reverseAnimations)
let count = count - 1
let deadline = DispatchTime.now() + duration
self.animate(withDuration: duration, animations: animations)
DispatchQueue.main.asyncAfter(deadline: deadline) {
var deadline2 = DispatchTime.now()
if autoreverses, let rev = reverseAnimations {
self.animate(withDuration: duration, animations: rev)
deadline2 = DispatchTime.now() + duration
}
DispatchQueue.main.asyncAfter(deadline: deadline2, execute: {
if !count.isEqual(to: .infinity) && count <= 0 {
completion?()
} else {
self.animate(forKey: key, duration: duration, repeatCount: CGFloat(count), autoreverses: autoreverses, animations: animations, reverseAnimations: reverseAnimations, completion: completion)
}
})
}
}
func stopAnimations() {
for key in self.animations.keys {
guard let rev = self.animations[key] else {return}
self.animationStopStates[key] = true
self.animate(forKey: key, duration: 0, repeatCount: 0, autoreverses: false, animations: {}, reverseAnimations: rev)
}
self.animations.removeAll()
}
private func setAnimations(_ key: String, reverse: (() -> Void)?) {
if self.animations[key] == nil {
self.animations[key] = reverse
}
}
}
How to use:
self.animate(forKey: "custom_anim1", duration: 1.0, repeatCount: .infinity, autoreverses: true, animations: {[unowned self] in
self.myButton.setHeight(100)
}, reverseAnimations: { [unowned self] in
self.myButton.setHeight(120)
}) {
print("Animation stopped!")
}
To stop all animations:
self.stopAnimations()