如何知道ios中的平移手势改变方向?

Jia*_*eng 10 objective-c ios swift

- (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--;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我平移手势向左移动时,然后向右移动.它会使用权利.我想知道如何知道平移手势改变方向?

小智 25

试试这个,

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

}
Run Code Online (Sandbox Code Playgroud)

  • 手指向左移动,然后向右移动,如何判断你改变方向? (2认同)

Luc*_*nzo 7

在 Swift 中,我创建了自己的扩展,易于使用,只要有 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))
    }

}
Run Code Online (Sandbox Code Playgroud)

你可以这样使用:

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")
    }
}
Run Code Online (Sandbox Code Playgroud)


dvd*_*blk 7

当你想要处理多个方向的移动时,这段代码扩展了Luca Davanzo用OptionSet的答案以获得正确的结果.

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

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