Skip to content

Instantly share code, notes, and snippets.

@philosopherdog
Created September 7, 2018 00:09
Show Gist options
  • Save philosopherdog/86f7064f79689d9f65bf15842e342e4d to your computer and use it in GitHub Desktop.
Save philosopherdog/86f7064f79689d9f65bf15842e342e4d to your computer and use it in GitHub Desktop.
W6D2-Keyframe-Animation
import PlaygroundSupport
import UIKit
class MyVC: UIViewController {
let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
view.addSubview(button)
button.setTitle("button", for: .normal)
button.addTarget(self, action: #selector(animateKeyFrames), for: .touchUpInside)
button.isEnabled = true
button.sizeToFit()
}
override func viewDidLayoutSubviews() {
button.center = view.center
}
@objc func animateKeyFrames() {
UIView.animateKeyframes(withDuration: 2.0, delay: 0, animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.25, animations: {
self.button.transform = CGAffineTransform(rotationAngle: 0.5*CGFloat.pi)
})
UIView.addKeyframe(withRelativeStartTime: 0.25, relativeDuration: 0.25, animations: {
self.button.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
})
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.25, animations: {
self.button.transform = CGAffineTransform(rotationAngle: 1.5*CGFloat.pi)
})
UIView.addKeyframe(withRelativeStartTime: 0.75, relativeDuration: 0.25, animations: {
self.button.transform = CGAffineTransform(rotationAngle: 2*CGFloat.pi)
})
})
}
}
let vc = MyVC()
PlaygroundPage.current.liveView = vc
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment