简体   繁体   中英

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?

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

}

This bit of code extends Luca Davanzo's answer with an OptionSet to get correct results when you want to handle moving in multiple directions.

Swift 3.1

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

    private func getDirectionBy(velocity: CGFloat, greater: PanGestureDirection, lower: PanGestureDirection) -> PanGestureDirection {
        if velocity == 0 {
            return []
        }
        return velocity > 0 ? greater : lower
    }

    public func direction(in view: UIView) -> PanGestureDirection {
        let velocity = self.velocity(in: view)
        let yDirection = getDirectionBy(velocity: velocity.y, greater: PanGestureDirection.Down, lower: PanGestureDirection.Up)
        let xDirection = getDirectionBy(velocity: velocity.x, greater: PanGestureDirection.Right, lower: PanGestureDirection.Left)
        return xDirection.union(yDirection)
    }
}

Use it like this:

let direction = panGestureRecognizer.direction(in: superview)
if direction.contains(.Left) && direction.contains(.Down) {
    // do stuff
} else if direction.contains(.Up) {
    // ...
}

In Swift I create my own extension easy to use everywhere you have a UIPanGestureRecognizer.

extension UIPanGestureRecognizer {

    enum GestureDirection {
        case Up
        case Down
        case Left
        case Right
    }

    /// Get current vertical direction
    ///
    /// - Parameter target: view target
    /// - Returns: current direction
    func verticalDirection(target target: UIView) -> GestureDirection {
        return self.velocityInView(target).y > 0 ? .Down : .Up
    }

    /// Get current horizontal direction
    ///
    /// - Parameter target: view target
    /// - Returns: current direction
    func horizontalDirection(target target: UIView) -> GestureDirection {
        return self.velocityInView(target).x > 0 ? .Right : .Left
    }

    /// Get a tuple for current horizontal/vertical direction
    ///
    /// - Parameter target: view target
    /// - Returns: current direction
    func versus(target target: UIView) -> (horizontal: GestureDirection, vertical: GestureDirection) {
        return (self.horizontalDirection(target: target), self.verticalDirection(target: target))
    }

}

You can use in this way:

override viewDidLoad() {
    super.viewDidLoad()
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(ViewController.didSwipeOnView(_:)))
    self.addGestureRecognizer(panGesture) 
}

func didSwipeOnView(gestureRecognizer: UIPanGestureRecognizer) {
    if case .Down = gestureRecognizer.verticalDirection(target: self) {
       print("Swiping down")
    } else {
       print("Swiping up")
    }
}

Swift 3

Create the gesture and attach it to your view:

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture))
yourView.addGestureRecognizer(panGesture)

Create a class variable that will hold the value of the latest direction:

var latestDirection: Int = 0

Panning on yourView will trigger the gesture handler where we check if pan direction was changed:

func handleGesture(gesture: UIPanGestureRecognizer) {
  let velocity = gesture.velocity(in: yourView)
  var currentDirection: Int = 0

  if velocity.x > 0 {
    print("panning right")
    currentDirection = 1
  } else {
    print("panning left")
    currentDirection = 2
  }

  // check if direction was changed
  if currentDirection != latestDirection {
    print("direction was changed")
  }

  latestDirection = currentDirection
}

This only handles direction change between left and right pan, but if someone wants to detect pan up/down, add this code to handleGesture function:

if velocity.y > 0 {
  print("panning down")
} else {
  print("panning up")
}

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM