[7 Masters] Wearables - WatchShaker

[7 Masters] Wearables - WatchShaker

42c9f7529d06c1e8f5da2c46ed4688d0?s=128

Ezequiel França

October 26, 2017
Tweet

Transcript

  1. 7Masters - Wearables Ezequiel França WatchShaker! ⌚

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

    @ FIAP Desenvolvedor iOS, Maker e open-source hacker. Ezequiel França
  3. None
  4. None
  5. Heurística?

  6. None
  7. None
  8. Let’s Shake

  9. None
  10. None
  11. http://indiatoday.intoday.in/technology/story/apple-watch- handshakes-nfc-gestures/1/448093.html

  12. None
  13. None
  14. lembrando rapidinho de protocolos delegates

  15. protocol SomeProtocol { func someTypeMethod() }

  16. protocol SomeProtocol { func someTypeMethod() } class SomeClass: SomeProtocol{ }

  17. protocol WatchShakerDelegate { func watchShakerDidShake(_ watchShaker: WatchShaker) func watchShaker(_ watchShaker:WatchShaker,

    didFailWith error: Error) }
  18. protocol WatchShakerDelegate { func didShake() func didFail(error: Error) }

  19. protocol WatchShakerDelegate { func watchShakerDidShake(_ watchShaker: WatchShaker) func watchShaker(_ watchShaker:WatchShaker,

    didFailWith error: Error) }
  20. 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
  21. init(shakeSensibility to:ShakeSensibility, delay time:Double) { self.threshold = to.rawValue self.delay =

    time self.motionManager = CMMotionManager() }
  22. ShakeSensibility

  23. enum ShakeSensibility: Double { case shakeSensibilitySoftest = 0.1 case shakeSensibilitySoft

    = 0.7 case shakeSensibilityNormal = 1.0 case shakeSensibilityHard = 1.2 case shakeSensibilityHardest = 2.0 }
  24. 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 }
  25. 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 }
  26. 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) }
  27. :) @ezefranca http://ezefranca.com