Skip to content

Instantly share code, notes, and snippets.

@danielCarlosCE
Created September 22, 2017 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielCarlosCE/257b510767f8fd058e18107c01fa8707 to your computer and use it in GitHub Desktop.
Save danielCarlosCE/257b510767f8fd058e18107c01fa8707 to your computer and use it in GitHub Desktop.
import UIKit
import PlaygroundSupport
var str = "Hello, playground"
let view = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))
view.backgroundColor = .red
let bottomLabel = UILabel()
bottomLabel.text = "Hello world!"
bottomLabel.sizeToFit()
bottomLabel.alpha = 0
view.addSubview(bottomLabel)
//animations
let fadeOutBottomLabel = {
bottomLabel.alpha = 0
}
let fadeInBottomLabel = {
bottomLabel.alpha = 1
}
let moveBottomLabelRight = {
bottomLabel.transform = CGAffineTransform(translationX: 30, y: 0)
}
struct Timeline {
var keyframes: [(wait: TimeInterval, thenRun: () -> Void, lasting: TimeInterval, curve: UIViewAnimationOptions, Timeline?)]
public init(_ keyframes: (wait: TimeInterval, thenRun: () -> Void, lasting: TimeInterval, curve: UIViewAnimationOptions, Timeline?)...) {
self.init(keyframes)
}
private init(_ keyframes: [(wait: TimeInterval, thenRun: () -> Void, lasting: TimeInterval, curve: UIViewAnimationOptions, Timeline?)]) {
print("another way")
self.keyframes = keyframes
}
func run() {
for frame in keyframes {
print("frame running")
UIView.animate(withDuration: frame.lasting, delay: frame.wait, options: frame.curve, animations: {
frame.thenRun()
}) { _ in
frame.4?.run()
}
}
}
static func then(_ keyframes: (wait: TimeInterval, thenRun: () -> Void, lasting: TimeInterval, curve: UIViewAnimationOptions, Timeline?)...) -> Timeline {
return Timeline(keyframes)
}
}
PlaygroundPage.current.liveView = view
let timeline = Timeline (
(wait: 1.0, thenRun: fadeInBottomLabel, lasting: 5.0, curve: .curveLinear, nil),
(wait: 1.5, thenRun: moveBottomLabelRight, lasting: 5.0, curve: .curveEaseInOut, .then(
(wait: 5.0, thenRun: fadeOutBottomLabel, lasting: 5.0, curve: .curveLinear, nil) )))
timeline.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment