在我的应用程序中,有一个图像view.Im将平移手势添加到该图像视图中。
这工作得很好。
图像视图处于横向模式。
我想在用户平移到右方向时增加标签中的计数,而在用户平移到左方向时减少计数。
我用谷歌搜索了很多,但没有找到解决这个问题的办法。
谁能告诉我如何检测用户平移的方向(左/右)?
发布于 2012-08-02 20:25:24
在手势识别器的目标选择器中,使用- (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--;
}
}附言:我直到大约才知道这个方法。3分钟前。谷歌最早的热门产品之一是苹果的官方文档。
发布于 2014-02-04 20:41:42
如下所示:
- (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");
}发布于 2016-06-08 17:21:01
在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
}
}
}https://stackoverflow.com/questions/11777281
复制相似问题