Slide 1

Slide 1 text

Swift Meetup SP Heurísticas e Swift: Do “NSProcessInfo” a um shake no Apple Watch Ezequiel França

Slide 2

Slide 2 text

Mecatrônica @ SENAI Automação Industrial @ IFSP Analise de Sistemas @ FIAP Desenvolvedor iOS, Maker e open-source hacker. Ezequiel França

Slide 3

Slide 3 text

https://www.youtube.com/watch?v=BoeRfMBVCGo

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Swift Meetup SP ❤

Slide 6

Slide 6 text

Heurísticas?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

http://mathwiki.cs.ut.ee/asymptotics/05_polynomial_complexity

Slide 15

Slide 15 text

Let’s Shake

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

lembrando rapidinho de protocolos delegates

Slide 21

Slide 21 text

protocol SomeProtocol { func someTypeMethod() }

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

protocol WatchShakerDelegate { func watchShakerDidShake(_ watchShaker: WatchShaker) func watchShaker(_ watchShaker:WatchShaker, didFailWith error: Error) }

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

protocol WatchShakerDelegate { func watchShakerDidShake(_ watchShaker: WatchShaker) func watchShaker(_ watchShaker:WatchShaker, didFailWith error: Error) }

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

init(shakeSensibility to:ShakeSensibility, delay time:Double) { self.threshold = to.rawValue self.delay = time self.motionManager = CMMotionManager() }

Slide 28

Slide 28 text

ShakeSensibility

Slide 29

Slide 29 text

enum ShakeSensibility: Double { case shakeSensibilitySoftest = 0.1 case shakeSensibilitySoft = 0.7 case shakeSensibilityNormal = 1.0 case shakeSensibilityHard = 1.2 case shakeSensibilityHardest = 2.0 }

Slide 30

Slide 30 text

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 }

Slide 31

Slide 31 text

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 }

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

:) @ezefranca

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

3 vouchers para o curso de SpriteKit (100% em Swift ❤)