session-ios/Signal/src/views/DirectionalPanGestureRecognizer.swift
2017-10-24 15:29:03 -07:00

39 lines
952 B
Swift

//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
import UIKit.UIGestureRecognizerSubclass
@objc
enum PanDirection: Int {
case vertical
case horizontal
}
@objc
class PanDirectionGestureRecognizer: UIPanGestureRecognizer {
let direction: PanDirection
init(direction: PanDirection, target: AnyObject, action: Selector) {
self.direction = direction
super.init(target: target, action: action)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
if state == .began {
let vel = velocity(in: view)
switch direction {
case .horizontal where fabs(vel.y) > fabs(vel.x):
state = .cancelled
case .vertical where fabs(vel.x) > fabs(vel.y):
state = .cancelled
default:
break
}
}
}
}