Implementing UIInterpolatingMotionEffect
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 | |
// 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