Skip to content

Instantly share code, notes, and snippets.

@rtking1993
Last active January 28, 2018 00:34
Embed
What would you like to do?
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