Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@marmelroy
Last active November 9, 2022 02:28
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save marmelroy/ed4bd675bd75c757ab7447d1b3488886 to your computer and use it in GitHub Desktop.
Save marmelroy/ed4bd675bd75c757ab7447d1b3488886 to your computer and use it in GitHub Desktop.
A quick example of UIViewPropertyAnimator
// Create a UIViewPropertyAnimator object. Here's a simple one with a UIKit animation curve:
let colorChange = UIViewPropertyAnimator(duration: 0.3, curve: .easeIn, animations: { [weak self] in
self?.view.backgroundColor = UIColor(red: 255.0/255.0, green: 80.0/255.0, blue: 43.0/255.0, alpha: 1.0)
})
// There's also support for easy spring-based animations - all you need to set is a damping ratio (a value between 0 and 1). Alternatively, you can create your own curves by adopting the UITimingCurveProvider protocol.
let alphaChange = UIViewPropertyAnimator(duration: 0.3, dampingRatio: 0.6, animations: { [weak self] in
self?.circleView.alpha = 0.0
})
// To run an animation
alphaChange.startAnimation()
//To pause an animator
alphaChange.pauseAnimation()
// For a gesture recognizer or delegate that reports every step of its progress (e.g. UIPanGestureRecognizer or a ScrollViewDidScroll) you can just apply the percentage directly to the UIViewPropertyAnimator object
@IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.view)
let translatedCenterY = view.center.y + translation.y
let progress = translatedCenterY / self.view.bounds.size.height
colorChange.fractionComplete = progress
}
// To invalidate
colorChange?.stopAnimation(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment