Created
April 4, 2018 16:13
-
-
Save akbsteam/223c19f48bce91e79d84bda0698679f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
typealias Anim = (UIView) -> Void | |
typealias Func = () -> Void | |
precedencegroup ForwardComposition | |
{ | |
associativity: left | |
higherThan: AssignmentPrecedence | |
} | |
infix operator >>>: ForwardComposition | |
func >>> (a: @escaping (Func?) -> Void, | |
b: @escaping (Func?) -> Void) -> (Func?) -> Void | |
{ | |
return { c in | |
a { b(c) } | |
} | |
} | |
func + (anim1: @escaping Anim, | |
anim2: @escaping Anim) -> Anim | |
{ | |
return { view in | |
anim1(view) | |
anim2(view) | |
} | |
} | |
func transform(_ transform: CGAffineTransform) -> (UIView) -> Void | |
{ | |
return { view in | |
view.transform = transform | |
} | |
} | |
func fade(to alpha: CGFloat) -> (UIView) -> Void | |
{ | |
return { view in | |
view.alpha = alpha | |
} | |
} | |
func animate(duration: TimeInterval, | |
animations: @escaping Func) -> (Func?) -> Void | |
{ | |
return { completion in | |
let animator = UIViewPropertyAnimator(duration: duration, curve: .linear, animations: animations) | |
animator.addCompletion({ _ in completion?() }) | |
animator.startAnimation() | |
} | |
} | |
func animator(_ views: [UIView], | |
_ animations: @escaping Anim, | |
_ duration: TimeInterval) -> (Func?) -> Void | |
{ | |
return animate(duration: duration, animations: { | |
views.forEach { animations($0) } | |
}) | |
} | |
class MyViewController : UIViewController | |
{ | |
override func loadView() | |
{ | |
let view = UIView() | |
view.backgroundColor = .white | |
let viewOne = UIView() | |
viewOne.frame = CGRect(x: 30, y: 30, width: 30, height: 30) | |
viewOne.backgroundColor = .black | |
let viewTwo = UIView() | |
viewTwo.frame = CGRect(x: 120, y: 30, width: 30, height: 30) | |
viewTwo.backgroundColor = .black | |
view.addSubview(viewOne) | |
view.addSubview(viewTwo) | |
self.view = view | |
let transformIn = transform(CGAffineTransform(scaleX: 2.0, y: 2.0)) | |
let transformOut = transform(CGAffineTransform(scaleX: 0.5, y: 0.5)) | |
let fadeIn = fade(to: 1) | |
let fadeOut = fade(to: 0) | |
let fadeInAnimations = fadeIn + transformIn | |
let fadeOutAnimations = fadeOut + transformOut | |
let initial = { | |
viewOne.alpha = 0 | |
viewTwo.alpha = 0 | |
} | |
initial() | |
view.setNeedsDisplay() | |
let f1 = animator([viewOne, viewTwo], fadeInAnimations, 0.5) | |
let f2 = animator([viewOne, viewTwo], fadeOutAnimations, 0.5) | |
let f3 = f1 >>> f2 >>> f1 >>> f2 >>> f1 >>> f2 | |
f3(nil) | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment