Forked from victorchee/PanDirectionGestureRecognizer.swift
Created
December 7, 2023 12:09
-
-
Save longpham2310/850ba96619dd151ed89dcbda10524790 to your computer and use it in GitHub Desktop.
Pan gesture recognizer with directions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
enum PanDirection { | |
case vertical | |
case horizontal | |
} | |
class PanDirectionGestureRecognizer: UIPanGestureRecognizer { | |
let direction: PanDirection | |
init(direction: PanDirection, target: Any?, 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 velo = velocity(in: view) | |
switch direction { | |
case .horizontal where abs(velo.y) > abs(velo.x): | |
state = .cancelled | |
case .vertical where abs(velo.x) > abs(velo.y): | |
state = .cancelled | |
default: | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment