Created
October 30, 2020 06:36
-
-
Save lhoward/bb4c265b6b92524bf2e6b0513e45fed6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| import Combine | |
| private let motionShaked = PassthroughSubject<Void, Never>() | |
| extension UIWindow { | |
| open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { | |
| if motion == .motionShake { | |
| motionShaked.send() | |
| } | |
| } | |
| } | |
| private struct ShakeViewModifier: ViewModifier { | |
| let action: (() -> Void) | |
| init(perform action: @escaping (() -> Void)) { | |
| self.action = action | |
| } | |
| func body(content: Content) -> some View { | |
| content | |
| .onAppear { | |
| } | |
| .onReceive(motionShaked.receive(on: RunLoop.main)) { | |
| action() | |
| } | |
| } | |
| } | |
| extension View { | |
| public func onShake(perform action: @escaping (() -> Void)) -> some View { | |
| return self.modifier(ShakeViewModifier(perform: action)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment