Skip to content

Instantly share code, notes, and snippets.

@marmelroy
Last active June 19, 2016 03:00
Show Gist options
  • Save marmelroy/43bb1d4ac64560fd708de217025627a3 to your computer and use it in GitHub Desktop.
Save marmelroy/43bb1d4ac64560fd708de217025627a3 to your computer and use it in GitHub Desktop.
// Create interpolation object
let colorChange = Interpolate(from: UIColor.whiteColor(), to: UIColor.redColor(), apply: { [weak self] (color) in
self?.view.backgroundColor = color
})
// 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 Interpolate 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.progress = progress
}
// For other types of gesture recognizers that only report a beginning and an end (e.g. a UILongPressGestureRecognizer), you can animate directly to a target progress value with a given duration.
@IBAction func handleLongPress(recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .Began:
colorChange.animate(1.0, duration: 0.3)
case .Cancelled, .Ended, .Failed:
colorChange.animate(0.0, duration: 0.3)
default: break
}
}
// To remove
colorChange.invalidate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment