Skip to content

Instantly share code, notes, and snippets.

@ChristianVinterly
Last active July 13, 2017 09:40
Show Gist options
  • Save ChristianVinterly/cfb4dc1bd0f173e047bd4c230cb6975e to your computer and use it in GitHub Desktop.
Save ChristianVinterly/cfb4dc1bd0f173e047bd4c230cb6975e to your computer and use it in GitHub Desktop.
Fun with MotionEffect
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