Created
January 31, 2024 11:35
-
-
Save MojtabaHs/77edd40ffad2b481c72dabd87501ce3c to your computer and use it in GitHub Desktop.
Enabling binding to the device motion sensor
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
struct MotionEffectModifier: ViewModifier { | |
@ObservedObject private(set) var manager = MotionManager.shared | |
@Binding var pitch: Double | |
@Binding var roll: Double | |
let magnitude: Double | |
func body(content: Content) -> some View { | |
content | |
.onAppear { manager.startUpdate() } | |
.onDisappear { manager.stopUpdate() } | |
.onChange(of: pitch) { pitch = $0 } | |
.onChange(of: roll) { roll = $0 } | |
} | |
} | |
extension View { | |
func motionEffect(pitch: Binding<Double>, roll: Binding<Double>, magnitude: Double = 1) -> some View { | |
modifier(MotionEffectModifier(pitch: pitch, roll: roll, magnitude: magnitude)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment