在iOS中检测PAN手势的方向

在我的应用程序中有一个图像视图。我将PAN手势添加到该图像视图。

这工作正常。

图像视图处于横向模式。

当用户平移到正确方向时,我想要增加标签计数,并在用户向左平移时减less该计数。

我GOOGLE了很多,但没有find一个解决scheme。

任何人都可以请帮助我如何检测用户正在平移的方向(左/右)?

在手势识别器的目标select器中,使用- (CGPoint)velocityInView:(UIView *)view;

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

Ps:我不知道这个方法,直到约。 3分钟前。 谷歌的第一个命中之一是官方的苹果文件。

像这样的东西:

 - (void)pan:(UIPanGestureRecognizer *)sender { typedef NS_ENUM(NSUInteger, UIPanGestureRecognizerDirection) { UIPanGestureRecognizerDirectionUndefined, UIPanGestureRecognizerDirectionUp, UIPanGestureRecognizerDirectionDown, UIPanGestureRecognizerDirectionLeft, UIPanGestureRecognizerDirectionRight }; static UIPanGestureRecognizerDirection direction = UIPanGestureRecognizerDirectionUndefined; switch (sender.state) { case UIGestureRecognizerStateBegan: { if (direction == UIPanGestureRecognizerDirectionUndefined) { CGPoint velocity = [sender velocityInView:recognizer.view]; BOOL isVerticalGesture = fabs(velocity.y) > fabs(velocity.x); if (isVerticalGesture) { if (velocity.y > 0) { direction = UIPanGestureRecognizerDirectionDown; } else { direction = UIPanGestureRecognizerDirectionUp; } } else { if (velocity.x > 0) { direction = UIPanGestureRecognizerDirectionRight; } else { direction = UIPanGestureRecognizerDirectionLeft; } } } break; } case UIGestureRecognizerStateChanged: { switch (direction) { case UIPanGestureRecognizerDirectionUp: { [self handleUpwardsGesture:sender]; break; } case UIPanGestureRecognizerDirectionDown: { [self handleDownwardsGesture:sender]; break; } case UIPanGestureRecognizerDirectionLeft: { [self handleLeftGesture:sender]; break; } case UIPanGestureRecognizerDirectionRight: { [self handleRightGesture:sender]; break; } default: { break; } } } case UIGestureRecognizerStateEnded: { direction = UIPanGestureRecognizerDirectionUndefined; break; } default: break; } } - (void)handleUpwardsGesture:(UIPanGestureRecognizer *)sender { NSLog(@"Up"); } - (void)handleDownwardsGesture:(UIPanGestureRecognizer *)sender { NSLog(@"Down"); } - (void)handleLeftGesture:(UIPanGestureRecognizer *)sender { NSLog(@"Left"); } - (void)handleRightGesture:(UIPanGestureRecognizer *)sender { NSLog(@"Right"); } 

我以前的答案在Swift中

 public enum Direction: Int { case Up case Down case Left case Right public var isX: Bool { return self == .Left || self == .Right } public var isY: Bool { return !isX } } public extension UIPanGestureRecognizer { public var direction: Direction? { let velocity = velocityInView(view) let vertical = fabs(velocity.y) > fabs(velocity.x) switch (vertical, velocity.x, velocity.y) { case (true, _, let y) where y < 0: return .Up case (true, _, let y) where y > 0: return .Down case (false, let x, _) where x > 0: return .Right case (false, let x, _) where x < 0: return .Left default: return nil } } } 

这是一个清理Swift 3版本,用法的例子:

 public enum PanDirection: Int { case up, down, left, right public var isVertical: Bool { return [.up, .down].contains(self) } public var isHorizontal: Bool { return !isVertical } } public extension UIPanGestureRecognizer { public var direction: PanDirection? { let velocity = self.velocity(in: view) let isVertical = fabs(velocity.y) > fabs(velocity.x) switch (isVertical, velocity.x, velocity.y) { case (true, _, let y) where y < 0: return .up case (true, _, let y) where y > 0: return .down case (false, let x, _) where x > 0: return .right case (false, let x, _) where x < 0: return .left default: return nil } } @IBAction func handlePanView(recognizer: UIPanGestureRecognizer) { if let direction = recognizer.direction { if direction.isVertical { //do what you want when pan is vertical } else if direction == .left { //do what you want when pan is left } } } 

在Swift 3上重写Adam Waite版本

 public enum PanDirection: Int { case up, down, left, right public var isX: Bool { return self == .left || self == .right } public var isY: Bool { return !isX } } extension UIPanGestureRecognizer { var direction: PanDirection? { let velocity = self.velocity(in: view) let vertical = fabs(velocity.y) > fabs(velocity.x) switch (vertical, velocity.x, velocity.y) { case (true, _, let y): return y < 0 ? .up : .down case (false, let x, _): return x > 0 ? .right : .left } } } 

请注意,上面的Adam的Swift版本有一个错误。 。如果y <0,则返回应该返回,如果y> 0,则返回.Down(它们颠倒过来)。

所以:

 //MARK: - Direction internal enum Direction { case Up case Down case Left case Right } //MARK: - UIPanGestureRecognizer internal extension UIPanGestureRecognizer { internal var direction: Direction? { let velocity = velocityInView(view) let isVertical = fabs(velocity.y) > fabs(velocity.x) switch (isVertical, velocity.x, velocity.y) { case (true, _, let y) where y < 0: return .Up case (true, _, let y) where y > 0: return .Down case (false, let x, _) where x > 0: return .Right case (false, let x, _) where x < 0: return .Left default: return nil } } }