Merge pull request #13 from makhocheung/main make didShake function run in UI threand
Merge pull request #13 from makhocheung/main
make didShake function run in UI threand
Simple motion detector for ⌚️ (watchOS) shake gesture.
WatchShaker is a watchOS helper to get your ⌚️ shake movements
Once you have your Swift package set up, adding WatchShaker as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies
Package.swift
dependencies: [ .package(url: "https://github.com/ezefranca/WatchShaker.git") ]
WatchShaker
import SwiftUI import WatchShaker struct ContentView: View { @StateObject var shaker:WatchShaker = WatchShaker(shakeSensibility: .shakeSensibilityNormal, delay: 0.2) var body: some View { VStack { if shaker.isShakerAvailable { Text("I'm shook! ⌚️⌚️⌚️") Text("\(shaker.shake.sensibility)") Text("\(shaker.shake.direction)") } } } }
import WatchKit import Foundation import WatchShaker class InterfaceController: WKInterfaceController { var shaker:WatchShaker = WatchShaker(shakeSensibility: .shakeSensibilityNormal, delay: 0.2) override func awake(withContext context: Any?) { super.awake(withContext: context) } override func willActivate() { super.willActivate() shaker.start() shaker.startWatchShakerUpdates = { shakeSensibility, error in guard error == nil else { print(error?.localizedDescription) return } print("I'm shook! ⌚️⌚️⌚️") } } override func didDeactivate() { super.didDeactivate() shaker.stop() } }
import WatchKit import Foundation class InterfaceController: WKInterfaceController { override func awake(withContext context: Any?) { super.awake(withContext: context) // Configure interface objects here. } var shaker:WatchShaker = WatchShaker(shakeSensibility: .shakeSensibilityNormal, delay: 0.2) override func willActivate() { super.willActivate() shaker.delegate = self shaker.start() } override func didDeactivate() { super.didDeactivate() shaker.stop() } } extension InterfaceController: WatchShakerDelegate { func watchShaker(_ watchShaker: WatchShaker, didShakeWith sensibility: ShakeSensibility) { print("I'm shook! ⌚️⌚️⌚️") } func watchShaker(_ watchShaker: WatchShaker, didFailWith error: Error) { print(error.localizedDescription) } }
If you interested you can get the direction of the shake on the didShakeWith method.
direction
didShakeWith
func watchShaker(_ watchShaker: WatchShaker, didShakeWith sensibility: ShakeSensibility, direction: ShakeDirection) { print("I'm shook! ⌚️⌚️⌚️ \(direction)") }
ShakeDirection is a simple enum who gives you up, down, left or right direction. The image bellow is the way used to determine what is each direction.
ShakeDirection
up
down
left
right
This gentleman bellow for example, is clearly doing a shake with ShakeDirection.shakeDirectionRight 😂
ShakeDirection.shakeDirectionRight
We would love for you to contribute to WatchShaker, check the LICENSE file for more info.
LICENSE
Docs: Here
Ezequiel França – @ezefranca and all awesome Contributors
Distributed under the MIT license. See LICENSE for more information.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
WatchShaker
WatchShaker is a watchOS helper to get your ⌚️ shake movements
Requirements
Installation
Swift Package Manager
Once you have your Swift package set up, adding WatchShaker as a dependency is as easy as adding it to the
dependencies
value of yourPackage.swift
.Manually
WatchShaker
folder in your project to your watch target.Usage example
@StateObject style
Closure Style
Delegate Style
Optional Directions
If you interested you can get the
direction
of the shake on thedidShakeWith
method.ShakeDirection
is a simple enum who gives youup
,down
,left
orright
direction. The image bellow is the way used to determine what is each direction.This gentleman bellow for example, is clearly doing a shake with
ShakeDirection.shakeDirectionRight
😂Contribute
We would love for you to contribute to WatchShaker, check the
LICENSE
file for more info.Meta
Docs: Here
Ezequiel França – @ezefranca and all awesome Contributors
Distributed under the MIT license. See
LICENSE
for more information.