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

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

42c9f7529d06c1e8f5da2c46ed4688d0?s=128

Ezequiel França

May 17, 2017
Tweet

Transcript

  1. Swift Meetup SP Heurísticas e Swift: Do “NSProcessInfo” a um

    shake no Apple Watch Ezequiel França
  2. Mecatrônica @ SENAI Automação Industrial @ IFSP Analise de Sistemas

    @ FIAP Desenvolvedor iOS, Maker e open-source hacker. Ezequiel França
  3. https://www.youtube.com/watch?v=BoeRfMBVCGo

  4. None
  5. Swift Meetup SP ❤

  6. Heurísticas?

  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. http://mathwiki.cs.ut.ee/asymptotics/05_polynomial_complexity

  15. Let’s Shake

  16. None
  17. None
  18. http://indiatoday.intoday.in/technology/story/apple-watch- handshakes-nfc-gestures/1/448093.html

  19. None
  20. lembrando rapidinho de protocolos delegates

  21. protocol SomeProtocol { func someTypeMethod() }

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

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

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

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

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

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

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

    = 0.7 case shakeSensibilityNormal = 1.0 case shakeSensibilityHard = 1.2 case shakeSensibilityHardest = 2.0 }
  30. 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 }
  31. 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 }
  32. 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) }
  33. :) @ezefranca

  34. None
  35. None
  36. 3 vouchers para o curso de SpriteKit (100% em Swift

    ❤)