Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know pan gesture change direction in ios?

- (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?

like image 610
Jiang Yufeng Avatar asked Jan 28 '16 07:01

Jiang Yufeng


People also ask

What is Pan gesture in IOS?

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.

How do you use UILongPressGestureRecognizer?

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.

What is UITapGestureRecognizer Swift?

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.


2 Answers

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
        }
    }
}
like image 60
Giang Avatar answered Oct 04 '22 20:10

Giang


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");
    }

}
like image 26
Manikandan D Avatar answered Oct 04 '22 19:10

Manikandan D