Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[7 Masters] Wearables - WatchShaker

[7 Masters] Wearables - WatchShaker

Ezequiel Santos

October 26, 2017
Tweet

More Decks by Ezequiel Santos

Other Decks in Programming

Transcript

  1. Mecatrônica @ SENAI Automação Industrial @ IFSP Analise de Sistemas

    @ FIAP Desenvolvedor iOS, Maker e open-source hacker. Ezequiel França
  2. class WatchShaker { public var delegate: WatchShakerDelegate? fileprivate var motionManager:

    CMMotionManager! fileprivate var lastShakeDate: Date? !// The threshold for how much acceleration needs to happen before an event will register. fileprivate var threshold:Double !// Time between shakes fileprivate var delay:Double = 0.1
  3. enum ShakeSensibility: Double { case shakeSensibilitySoftest = 0.1 case shakeSensibilitySoft

    = 0.7 case shakeSensibilityNormal = 1.0 case shakeSensibilityHard = 1.2 case shakeSensibilityHardest = 2.0 }
  4. public func start(delay accelerometerUpdateInterval:Double = 0.02) { guard motionManager.isAccelerometerAvailable else

    { return } motionManager.accelerometerUpdateInterval = accelerometerUpdateInterval let motionQueue = OperationQueue() motionManager.startAccelerometerUpdates(to: motionQueue) { (accelerometerData, err) -> Void in guard err == nil else { self.delegate?.watchShaker(self, didFailWith: err!) return }
  5. guard let data = accelerometerData else { let e =

    NSError(domain: "No accelerometer data", code: 666, userInfo: ["No accelerometer data":"info"]) self.delegate?.watchShaker(self, didFailWith: e) return }
  6. let valueX = fabs(data.acceleration.x) let valueY = fabs(data.acceleration.y) let maxValue

    = valueX > valueY ? valueX : valueY if maxValue > self.threshold { if let lastDate = self.lastShakeDate { if Date().compare(lastDate.addingTimeInterval(self.delay)) !== .orderedDescending { self.lastShakeDate = Date() self.delegate!?.watchShakerDidShake(self) } return } self.lastShakeDate = Date() self.delegate!?.watchShakerDidShake(self) }