Last active
April 20, 2020 12:22
-
-
Save pyxn/11869563aaf2795c6c3a612ab6327022 to your computer and use it in GitHub Desktop.
Parallax motion effect from UIKit repurposed for SwiftUI
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 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