我在cellForRowAtIndexPath
UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeFrom:)];
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight];//|UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:gestureR];
它工作正常。但我想要UISwipeGestureRecognizerDirectionLeft
所以像这样添加
[gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)];
当我检查方向和状态时,我总是得到3 = 3
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"%d = %d",recognizer.direction,recognizer.state);
}
如果我只申请一个手势,它可以正常工作。我试着逐个添加两个手势。但它只会响应一个手势。
如何添加第二个手势。我直接将一个手势添加到TableView另一个手势,但现在使用。
答案 0 :(得分:23)
试试这个
UISwipeGestureRecognizer* gestureR; gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease]; gestureR.direction = UISwipeGestureRecognizerDirectionLeft; [view addGestureRecognizer:gestureR]; gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease]; gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default [view addGestureRecognizer:gestureR];
如果您想在左右滑动中处理不同的功能,只需更改选择器。
答案 1 :(得分:7)
如果使用
,最好不要两次分配UISwipeGestureRecognizer* recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft+UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:recognizer];
并在动作中获取滑动方向:
-(void)handleSwipe:(UISwipeGestureRecognizer *) sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
//do something
}
else //if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
//do something
}
}
答案 2 :(得分:5)
我知道你问这个问题已经过了很久了。
但请在您的问题中再次阅读以下行。
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];
您是否意识到您添加了UISwipeGestureRecognizerDirectionRight
。两次!!
:d