Created
October 24, 2024 01:12
-
-
Save takoikatakotako/a6d5d63f0a9371982028eea049853c1f 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 | |
| struct ContentView: View { | |
| @State var message = "Shake Me" | |
| var body: some View { | |
| Text(message) | |
| .onReceive(NotificationCenter.default.publisher(for: .deviceDidShakeNotification)) { _ in | |
| message = "Device Did Shake" | |
| } | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
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 Foundation | |
| extension NSNotification.Name { | |
| public static let deviceDidShakeNotification = NSNotification.Name("DeviceDidShakeNotification") | |
| } |
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 Foundation | |
| extension UIWindow { | |
| open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { | |
| super.motionEnded(motion, with: event) | |
| NotificationCenter.default.post(name: .deviceDidShakeNotification, object: event) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment