judging by velocity
is too sensitive to me, user easily mixed tapping screen with panning a small part.
So I use custom UIPanGestureRecognizer
. Swift 5
enum Orientation{
case lhs, rhs, `default`
}
class HPan: UIPanGestureRecognizer {
var first: CGPoint?
var orient = .default
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesBegan(touches, with: event)
state = .began
first = touches.first?.location(in: view?.window)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)
if state == .cancelled{
return
}
if let location = touches.first?.location(in: view?.window), let begin = first{
let magicDistance: CGFloat = 20
if location.x - begin.x > magicDistance{
orient = .rhs
state = .ended
}
else if begin.x - location.x > magicDistance{
orient = .lhs
state = .ended
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesEnded(touches, with: event)
state = .ended
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesCancelled(touches, with: event)
state = .cancelled
}
override func reset() {
super.reset()
state = .possible
isRight = false
orient = .default
}
}
Call like this:
@objc func push(_ gesture: RightPan){
if gesture.state == UIGestureRecognizer.State.ended{
switch gesture.orient{
case .lhs:
counter -= 1
case .rhs:
counter += 1
default:
()
}
}
}