Last active
March 10, 2020 04:02
-
-
Save NilStack/dee4247b04541762fecc0b93bdc0d251 to your computer and use it in GitHub Desktop.
UIViewPropertyAnimator example
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
import UIKit | |
import PlaygroundSupport | |
class AnimatorExampleViewController: UIViewController { | |
let startButton = UIButton() | |
let pauseButton = UIButton() | |
let stopButton = UIButton() | |
let resetButton = UIButton() | |
let reverseButton = UIButton() | |
let slider = UISlider() | |
let rect = UIView() | |
let propertyAnimator: UIViewPropertyAnimator = UIViewPropertyAnimator(duration: 5.0, curve: UIViewAnimationCurve.easeInOut) | |
override func viewDidLoad() { | |
view.backgroundColor = UIColor.white | |
rect.frame = CGRect(x: 0, y: 50, width: 100, height: 100) | |
rect.backgroundColor = UIColor.red | |
view.addSubview(rect) | |
startButton.frame = CGRect(x: 50, y: 300, width: 60, height: 30) | |
startButton.setTitle("Start", for: UIControlState.normal) | |
startButton.setTitleColor(UIColor.black, for: UIControlState.normal) | |
startButton.setTitleColor(UIColor.gray, for: UIControlState.disabled) | |
startButton.addTarget(self, action: #selector(startAnimation), for: UIControlEvents.touchUpInside) | |
view.addSubview(startButton) | |
pauseButton.frame = CGRect(x: 120, y: 300, width: 60, height: 30) | |
pauseButton.setTitle("Pause", for: UIControlState.normal) | |
pauseButton.setTitleColor(UIColor.black, for: UIControlState.normal) | |
pauseButton.setTitleColor(UIColor.gray, for: UIControlState.disabled) | |
pauseButton.isEnabled = false | |
pauseButton.addTarget(self, action: #selector(pauseAnimation), for: UIControlEvents.touchUpInside) | |
view.addSubview(pauseButton) | |
stopButton.frame = CGRect(x: 190, y: 300, width: 60, height: 30) | |
stopButton.setTitle("Stop", for: UIControlState.normal) | |
stopButton.setTitleColor(UIColor.black, for: UIControlState.normal) | |
stopButton.setTitleColor(UIColor.gray, for: UIControlState.disabled) | |
stopButton.isEnabled = false | |
stopButton.addTarget(self, action: #selector(stopAnimation), for: UIControlEvents.touchUpInside) | |
view.addSubview(stopButton) | |
resetButton.frame = CGRect(x: 260, y: 300, width: 60, height: 30) | |
resetButton.setTitle("Reset", for: UIControlState.normal) | |
resetButton.setTitleColor(UIColor.black, for: UIControlState.normal) | |
resetButton.setTitleColor(UIColor.gray, for: UIControlState.disabled) | |
resetButton.isEnabled = false | |
resetButton.addTarget(self, action: #selector(resetAnimation), for: UIControlEvents.touchUpInside) | |
view.addSubview(resetButton) | |
reverseButton.frame = CGRect(x: 50, y: 360, width: 80, height: 30) | |
reverseButton.setTitle("Reverse", for: UIControlState.normal) | |
reverseButton.setTitleColor(UIColor.black, for: UIControlState.normal) | |
reverseButton.setTitleColor(UIColor.gray, for: UIControlState.disabled) | |
reverseButton.isEnabled = false | |
reverseButton.addTarget(self, action: #selector(reverseAnimation), for: UIControlEvents.touchUpInside) | |
view.addSubview(reverseButton) | |
slider.frame = CGRect(x: 50, y: 400, width: 200, height: 30) | |
slider.minimumValue = 0 | |
slider.maximumValue = 1.0 | |
slider.isContinuous = true | |
slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: UIControlEvents.valueChanged) | |
view.addSubview(slider) | |
propertyAnimator.addAnimations { | |
self.rect.center.x = 300 | |
} | |
} | |
func startAnimation() { | |
startButton.isEnabled = false | |
pauseButton.isEnabled = true | |
stopButton.isEnabled = true | |
reverseButton.isEnabled = true | |
propertyAnimator.startAnimation() | |
} | |
func pauseAnimation() { | |
pauseButton.isEnabled = false | |
startButton.isEnabled = true | |
stopButton.isEnabled = true | |
reverseButton.isEnabled = false | |
propertyAnimator.pauseAnimation() | |
slider.value = Float(propertyAnimator.fractionComplete) | |
} | |
func stopAnimation() { | |
stopButton.isEnabled = false | |
startButton.isEnabled = false | |
pauseButton.isEnabled = false | |
resetButton.isEnabled = true | |
reverseButton.isEnabled = false | |
propertyAnimator.stopAnimation(false) | |
propertyAnimator.finishAnimation(at: .current) | |
slider.value = Float(propertyAnimator.fractionComplete) | |
} | |
func resetAnimation() { | |
self.rect.center.x = 50 | |
propertyAnimator.addAnimations { | |
self.rect.center.x = 300 | |
} | |
startButton.isEnabled = true | |
resetButton.isEnabled = false | |
reverseButton.isEnabled = false | |
slider.value = 0 | |
} | |
func reverseAnimation() { | |
if (propertyAnimator.isRunning) { | |
propertyAnimator.isReversed = true | |
} | |
reverseButton.isEnabled = false | |
} | |
func sliderValueChanged(_ sender: UISlider) { | |
if propertyAnimator.isRunning { | |
propertyAnimator.pauseAnimation() | |
} | |
propertyAnimator.fractionComplete = CGFloat(sender.value) | |
} | |
} | |
PlaygroundPage.current.liveView = AnimatorExampleViewController() // containerView |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment