Last active
July 13, 2017 09:40
-
-
Save ChristianVinterly/cfb4dc1bd0f173e047bd4c230cb6975e to your computer and use it in GitHub Desktop.
Fun with MotionEffect
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
func addParallaxMotionEffects(tiltValue: CGFloat = 0.25, panValue: CGFloat = 5) { | |
var xTilt = UIInterpolatingMotionEffect() | |
var yTilt = UIInterpolatingMotionEffect() | |
var xPan = UIInterpolatingMotionEffect() | |
var yPan = UIInterpolatingMotionEffect() | |
let motionGroup = UIMotionEffectGroup() | |
xTilt = UIInterpolatingMotionEffect( | |
keyPath: "layer.transform.rotation.y", | |
type: .tiltAlongHorizontalAxis | |
) | |
xTilt.minimumRelativeValue = -tiltValue | |
xTilt.maximumRelativeValue = tiltValue | |
yTilt = UIInterpolatingMotionEffect( | |
keyPath: "layer.transform.rotation.x", | |
type: .tiltAlongVerticalAxis | |
) | |
yTilt.minimumRelativeValue = -tiltValue | |
yTilt.maximumRelativeValue = tiltValue | |
xPan = UIInterpolatingMotionEffect( | |
keyPath: "center.x", | |
type: .tiltAlongHorizontalAxis | |
) | |
xPan.minimumRelativeValue = -panValue | |
xPan.maximumRelativeValue = panValue | |
yPan = UIInterpolatingMotionEffect( | |
keyPath: "center.y", | |
type: .tiltAlongVerticalAxis | |
) | |
yPan.minimumRelativeValue = -panValue | |
yPan.maximumRelativeValue = panValue | |
motionGroup.motionEffects = [xTilt, yTilt, xPan, yPan] | |
addMotionEffect(motionGroup) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment