Skip to content

Instantly share code, notes, and snippets.

@pyxn
Last active April 20, 2020 12:22
Show Gist options
  • Save pyxn/11869563aaf2795c6c3a612ab6327022 to your computer and use it in GitHub Desktop.
Save pyxn/11869563aaf2795c6c3a612ab6327022 to your computer and use it in GitHub Desktop.
Parallax motion effect from UIKit repurposed for SwiftUI
import SwiftUI
struct Motion {
let motionEffectGroup: UIMotionEffectGroup = {
let xMotion: UIInterpolatingMotionEffect = {
let min = CGFloat(-30)
let max = CGFloat(30)
let xMotionSetting = UIInterpolatingMotionEffect(keyPath: "layer.transform.translation.x", type: .tiltAlongHorizontalAxis)
xMotionSetting.maximumRelativeValue = max
xMotionSetting.minimumRelativeValue = min
return xMotionSetting
}()
let yMotion: UIInterpolatingMotionEffect = {
let min = CGFloat(-30)
let max = CGFloat(30)
let xMotionSetting = UIInterpolatingMotionEffect(keyPath: "layer.transform.translation.y", type: .tiltAlongVerticalAxis)
xMotionSetting.maximumRelativeValue = max
xMotionSetting.minimumRelativeValue = min
return xMotionSetting
}()
let motionEffectGroupSetting = UIMotionEffectGroup()
motionEffectGroupSetting.motionEffects = [xMotion,yMotion]
return motionEffectGroupSetting
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment