- (void)panRecognized:(UIPanGestureRecognizer *)rec
{
CGPoint vel = [rec velocityInView:self.view];
if (vel.x > 0)
{
// user dragged towards the right
counter++;
}
else
{
// user dragged towards the left
counter--;
}
}
When I pan gesture move to left ,then move to right. it will use right . I want to How to know pan gesture change direction?
A pan gesture occurs any time a person moves one or more fingers around the screen. A screen-edge pan gesture is a specialized pan gesture that originates from the edge of the screen. Use the UIPanGestureRecognizer class for pan gestures and the UIScreenEdgePanGestureRecognizer class for screen-edge pan gestures.
UILongPressGestureRecognizer is a concrete subclass of UIGestureRecognizer . The user must press one or more fingers on a view and hold them there for a minimum period of time before the action triggers. While down, the userʼs fingers canʼt move more than a specified distance or the gesture fails.
UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer . For gesture recognition, the specified number of fingers must tap the view a specified number of times. Although taps are discrete gestures, they're discrete for each state of the gesture recognizer.
Swift 5.x
extension UIPanGestureRecognizer {
public struct PanGestureDirection: OptionSet {
public let rawValue: UInt8
public init(rawValue: UInt8) {
self.rawValue = rawValue
}
static let Up = PanGestureDirection(rawValue: 1 << 0)
static let Down = PanGestureDirection(rawValue: 1 << 1)
static let Left = PanGestureDirection(rawValue: 1 << 2)
static let Right = PanGestureDirection(rawValue: 1 << 3)
}
public func direction(in view: UIView) -> PanGestureDirection {
let velocity = self.velocity(in: view)
let isVerticalGesture = abs(velocity.y) > abs(velocity.x)
if isVerticalGesture {
return velocity.y > 0 ? .Down : .Up
} else {
return velocity.x > 0 ? .Right : .Left
}
}
}
Try this,
- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint velocity = [gestureRecognizer velocityInView:yourView];
if(velocity.x > 0)
{
NSLog(@"gesture moving right");
}
else
{
NSLog(@"gesture moving left");
}
if(velocity.y > 0)
{
NSLog(@"gesture moving Up");
}
else
{
NSLog(@"gesture moving Bottom");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With