Skip to content

Instantly share code, notes, and snippets.

@Blackjacx
Created March 31, 2022 08:37
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 Blackjacx/b600edf9254b7c750490835e0837b092 to your computer and use it in GitHub Desktop.
Save Blackjacx/b600edf9254b7c750490835e0837b092 to your computer and use it in GitHub Desktop.
iOS Parallax Effect as UIView Extension
extension UIView {
func applyParallax() {
let amount = 10
let horizontal = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
horizontal.minimumRelativeValue = -amount
horizontal.maximumRelativeValue = amount
let vertical = UIInterpolatingMotionEffect(keyPath: "center.y", type: .tiltAlongVerticalAxis)
vertical.minimumRelativeValue = -amount
vertical.maximumRelativeValue = amount
let group = UIMotionEffectGroup()
group.motionEffects = [horizontal, vertical]
addMotionEffect(group)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment