Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

 [#2 Swift Meetup SP - 2017] Heurísticas e Swif...

 [#2 Swift Meetup SP - 2017] Heurísticas e Swift: Do “NSProcessInfo” à um shake no Apple Watch

Ezequiel Santos

May 17, 2017
Tweet

More Decks by Ezequiel Santos

Other Decks in Technology

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) }