Skip to content
Snippets Groups Projects
Commit 36e598d5 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Add ShakeViewModifier

parent c8106cfd
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!101Messenger example - logging
import SwiftUI
struct ShakeViewModifier: ViewModifier {
var action: () -> Void
func body(content: Content) -> some View {
content.onReceive(
NotificationCenter.default.publisher(
for: UIDevice.deviceDidShakeNotification
),
perform: { _ in
action()
}
)
}
}
extension View {
public func onShake(perform action: @escaping () -> Void) -> some View {
modifier(ShakeViewModifier(action: action))
}
}
extension UIDevice {
static let deviceDidShakeNotification = Notification.Name(
rawValue: "deviceDidShakeNotification"
)
}
extension UIWindow {
open override func motionEnded(
_ motion: UIEvent.EventSubtype,
with event: UIEvent?
) {
super.motionEnded(motion, with: event)
guard motion == .motionShake else { return }
NotificationCenter.default.post(
name: UIDevice.deviceDidShakeNotification,
object: nil
)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment