Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 24, 2024 01:12
Show Gist options
  • Select an option

  • Save takoikatakotako/a6d5d63f0a9371982028eea049853c1f to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/a6d5d63f0a9371982028eea049853c1f to your computer and use it in GitHub Desktop.
端末のシェイクを検知する
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()
}
import Foundation
extension NSNotification.Name {
public static let deviceDidShakeNotification = NSNotification.Name("DeviceDidShakeNotification")
}
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