-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds shake gesture view modifier (#64)
Adds shake gesture view modifier implementing override on motionEnded on UIWindow using NotificationCenter to broadcast motion detection
- Loading branch information
1 parent
7746162
commit b62dd45
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Sources/UIExtensions/Extensions/UIWindow+MotionEnded.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright © 2023 Lautsprecher Teufel GmbH. All rights reserved. | ||
|
||
import UIKit | ||
|
||
extension NSNotification.Name { | ||
public static let didShakeDevice = NSNotification.Name("DidShakeDeviceNotification") | ||
} | ||
|
||
extension UIWindow { | ||
override open func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { | ||
super.motionEnded(motion, with: event) | ||
|
||
switch event?.subtype { | ||
case .motionShake: | ||
NotificationCenter.default.post(name: .didShakeDevice, object: event) | ||
default: break | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Sources/UIExtensions/ViewModifiers/ShakeGestureViewModifier.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright © 2023 Lautsprecher Teufel GmbH. All rights reserved. | ||
|
||
import SwiftUI | ||
|
||
struct ShakeGestureViewModifier: ViewModifier { | ||
let action: () -> Void | ||
|
||
func body(content: Content) -> some View { | ||
content.onReceive(NotificationCenter.default.publisher(for: .didShakeDevice)) { _ in | ||
action() | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
public func onShakeGesture(perform action: @escaping () -> Void) -> some View { | ||
self.modifier(ShakeGestureViewModifier(action: action)) | ||
} | ||
} |