UISwipeGestureIdentiizer 的设置方向

我想在基于视图的 iPhone 项目中添加简单的滑动手势识别。所有方向(右,下,左,上)的手势都应该被识别。

UISwipeGestureIdentiizer 的文档中说明了这一点:

可以通过使用按位 OR 操作数指定多个 UISwipeGesture识别方向常数来指定多个方向。

然而对我来说,它并不起作用。当所有四个方向都被“或”时,只有左右滑动才能被识别。

- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
[super viewDidLoad];
}


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
}

我通过在视图中添加四个识别器修复了这个问题,但是我很好奇为什么它不能像文档中广告的那样工作呢?

- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];


[super viewDidLoad];
}


-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
}
90972 次浏览

我注意到左/右和上/下手势是成对工作的,所以您只需要指定两个手势识别器。而且医生似乎是错的。

好像有个窃听器。您可以像以前那样指定允许的方向。但是,当您尝试访问 真的方向,触发滑动的行动选择器方法,您仍然得到位掩码,您最初设置(为允许的方向)。

这意味着当允许多个方向时,对实际方向的检查总是失败。 当您在选择器方法(即 -(void)scrollViewSwiped:(UISwipeGestureRecognizer *)recognizer)中输出“ direct”的值时,您可以非常容易地亲眼看到它。

向苹果公司提交了一份错误报告(# 8276386)。

[更新]我从苹果公司得到一个答复,说这种行为是按照预期的方式工作的。

例如,在表格视图中,你可以在表格视图单元格中向左或向右滑动以触发“删除”(这将使滑动手势的方向设置为向左或向右)

这意味着原始的工作区是它应该被使用的方式。方向属性只能用于正确识别手势,而不能用于成功识别时比较触发识别的实际方向的方法。

嗯,奇怪,这对我来说很完美,我也做同样的事情

我觉得你应该看看

委托方法

- (BOOL)gestureRecognizerShouldBegin:(UISwipeGestureRecognizer *)gestureRecognizer {
// also try to look what's wrong with gesture
NSLog(@"should began gesture %@", gestureRecognizer);
return YES;
}

在日志中,你必须看到这样的内容:

应该开始手势; target = < (action = actionForUpDownSwipeGestureIdentiizer: ,target =) > ; direct = up,down,left,right >

那太糟糕了,我通过增加两个手势解决了这个问题,就像佬司提到的那样,而且效果非常好... ..。

1)左/右 2)上/下




UISwipeGestureRecognizer *swipeLeftRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[swipeLeftRight setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:swipeLeftRight];


UISwipeGestureRecognizer *swipeUpDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];
[self.view addGestureRecognizer:swipeUpDown];

如果您使用 Xcode 4.2,您可以添加手势识别器@故事板,然后将 GUI 手势识别器链接到 IBActions。

您可以在实用工具窗格的对象库(右窗格的底部)中找到手势识别器。

然后只需要控制-拖动到适当的操作。

使用这个,它应该是位操作

   gesture.direction & UISwipeGestureRecognizerDirectionUp ||
gesture.direction & UISwipeGestureRecognizerDirectionDown

下面是 UISwipeGestureIdentiizer 使用的代码示例。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//add gesture recognizer. The 'direction' property of UISwipeGestureRecognizer only sets the allowable directions. It does not return to the user the direction that was actaully swiped. Must set up separate gesture recognizers to handle the specific directions for which I want an outcome.
UISwipeGestureRecognizer *gestureRight;
UISwipeGestureRecognizer *gestureLeft;
gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];//direction is set by default.
gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];//need to set direction.
[gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
//[gesture setNumberOfTouchesRequired:1];//default is 1
[[self view] addGestureRecognizer:gestureRight];//this gets things rolling.
[[self view] addGestureRecognizer:gestureLeft];//this gets things rolling.
}

向右和向左是用来执行基于向左或向右滑动的特定活动的方法,例如:

- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
NSLog(@"Right Swipe received.");//Lets you know this method was called by gesture recognizer.
NSLog(@"Direction is: %i", gesture.direction);//Lets you know the numeric value of the gesture direction for confirmation (1=right).
//only interested in gesture if gesture state == changed or ended (From Paul Hegarty @ standford U
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {


//do something for a right swipe gesture.
}
}

如果您想要它检测所有四个方向,您将需要创建四个实例,就像您在工作区中所做的那样。

原因如下: 创建的 UISwipeGestureIdentiizer 实例与作为发送方传递给选择器的实例相同。因此,如果将它设置为识别所有四个方向,那么对于 sgr.direction == xxx将返回 true,其中 xxx 是四个方向中的任何一个。

下面是一个涉及较少代码的 替代性解决办法(假设使用 ARC) :

for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) {
UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
sgr.direction = d;
[self.view addGestureRecognizer:sgr];
}
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

现在这个函数是 dswip 函数

- (void) didSwipe:(UISwipeGestureRecognizer *)recognizer{
if([recognizer direction] == UISwipeGestureRecognizerDirectionLeft){
//Swipe from right to left
//Do your functions here
}else{
//Swipe from left to right
//Do your functions here
}
}

这简直要把我逼疯了。我终于找到了一个可靠的方法来获得多个滑动手势识别器。

如果你的“动作”选择器的名字在多个 wipeGestureIdentity 中是相同的,那么看起来 iOS 系统中有一个 bug。如果你只是用不同的名字命名它们,比如 handleLeftSwipeFrom 和 handleRightSwipeFrom,那么一切都可以正常工作。

UISwipeGestureRecognizer *recognizer;


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];


recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)];
Updown.delegate=self;
[Updown setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[overLayView addGestureRecognizer:Updown];


UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleGestureNext:)];
LeftRight.delegate=self;
[LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight];
[overLayView addGestureRecognizer:LeftRight];
overLayView.userInteractionEnabled=NO;




-(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"Swipe Recevied");
//Left
//Right
//Top
//Bottom
}

Swift 2.1

我不得不使用以下方法

    for var x in [
UISwipeGestureRecognizerDirection.Left,
UISwipeGestureRecognizerDirection.Right,
UISwipeGestureRecognizerDirection.Up,
UISwipeGestureRecognizerDirection.Down
] {
let r = UISwipeGestureRecognizer(target: self, action: "swipe:")
r.direction = x
self.view.addGestureRecognizer(r)
}