Skip to content

Instantly share code, notes, and snippets.

@lhoward
Created October 30, 2020 06:36
Show Gist options
  • Select an option

  • Save lhoward/bb4c265b6b92524bf2e6b0513e45fed6 to your computer and use it in GitHub Desktop.

Select an option

Save lhoward/bb4c265b6b92524bf2e6b0513e45fed6 to your computer and use it in GitHub Desktop.
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