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

Custom Gesture Recognizer on iOS

Tachibana Kaoru
April 27, 2020
1.3k

Custom Gesture Recognizer on iOS

Tachibana Kaoru

April 27, 2020
Tweet

Transcript

  1. UITouch open class UITouch : NSObject { open var timestamp:

    TimeInterval { get } open var tapCount: Int { get } open var type: UITouch.TouchType { get } open func location(in view: UIView?) -> CGPoint open func previousLocation(in view: UIView?) -> CGPoint }
  2. velocity vector with UITouch location previousLocation (x1 , y1 )

    (x2 , y2 ) ( x2 − x1 t2 − t1 , y2 − y1 t2 − t1 ) 2఺ؒͷ଎౓ϕΫτϧ
  3. velocity vector with UITouch location previousLocation (x1 , y1 )

    (x2 , y2 ) ( x2 − x1 t2 − t1 , y2 − y1 t2 − t1 ) 2఺ؒͷ଎౓ϕΫτϧ (x2 − x1 , y2 − y1 ) 2఺ؒͷ୯Ґ࣌ؒ͋ͨΓͷ଎౓ϕΫτϧ
  4. struct Velocity { var vx: CGFloat var vy: CGFloat }

    extension UITouch{ func velocity(in view: UIView?) -> Velocity{ let location = location(in: view) let previousLocation = previousLocation(in: view) let distanceX = location.x - previousLocation.x let distanceY = location.y - previousLocation.y let velocity = Velocity(vx: distanceX, vy: distanceY) return velocity }
  5. gradient of UITouch location previousLocation (x1 , y1 ) (x2

    , y2 ) θ vx vy tan θ = vy vx θ = tan−1 vy vx
  6. struct Velocity { var vx: CGFloat var vy: CGFloat var

    gradient: CGFloat{ // ࣮ࡍʹ͸ɺvyɺvx͕0ͷ࣌ͷॲཧ͓Αͼɺgradient஋͕ 0-2πʹͳΔΑ͏ͳ // ਖ਼نԽΛߦ͍·͢ var calcGradient = atan(vy / vx) return calcGradient }
  7. UIGestureRecognizer.State open class UIGestureRecognizer : NSObject { // the current

    state of the gesture recognizer open var state: UIGestureRecognizer.State { get } }
  8. class MyGestureRecognizer: UIGestureRecognizer{ override func touchesBegan(_ touches: Set<UITouch>, with event:

    UIEvent) { } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) { } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) { } override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) { } override func reset() { } }
  9. P F P P P P R F F touchesBegan

    touchesMoved touchesEnd
  10. P F P P P P R F F touchesBegan

    touchesMoved touchesEnd
  11. P F P P P F touchesBegan touchesMoved touchesEnd touchesEnd

    touchesMoved touchesMoved touchesBegan R F P P