Skip to content

Instantly share code, notes, and snippets.

@rtking1993
Last active January 28, 2018 00:34
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 rtking1993/e0692e99f0dde15b3cbb32eee0ef8ca0 to your computer and use it in GitHub Desktop.
Save rtking1993/e0692e99f0dde15b3cbb32eee0ef8ca0 to your computer and use it in GitHub Desktop.
Implementing UIInterpolatingMotionEffect
import UIKit
// Extension
extension UIView {
func motionEffect(intensity: Int) {
// Set vertical effect
let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .tiltAlongVerticalAxis)
verticalMotionEffect.minimumRelativeValue = -intensity
verticalMotionEffect.maximumRelativeValue = intensity
// Set horizontal effect
let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
horizontalMotionEffect.minimumRelativeValue = -intensity
horizontalMotionEffect.maximumRelativeValue = intensity
// Create group to combine both
let group = UIMotionEffectGroup()
group.motionEffects = [horizontalMotionEffect, verticalMotionEffect]
addMotionEffect(group)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment