13

我在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 另一个手势到单元格,但现在使用。

4

3 回答 3

23

尝试这个

UISwipeGestureRecognizer* 手势R;
手势R = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
手势R.direction = UISwipeGestureRecognizerDirectionLeft;
[查看 addGestureRecognizer:gestureR];

手势R = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
手势R.direction = UISwipeGestureRecognizerDirectionRight; // 默认
[查看 addGestureRecognizer:gestureR];

如果您想处理左右滑动的不同功能,只需更改选择器即可。

于 2011-12-19T16:24:56.803 回答
7

而不是两次alloc,如果你使用会更好

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
     }
}
于 2013-11-30T07:32:33.773 回答
5

我知道自从你问这个问题已经很久了。但是请尝试在您的问题中再次阅读以下行。 [gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];

你意识到你添加了UISwipeGestureRecognizerDirectionRight. 两次!!

:D

于 2013-05-09T06:42:13.867 回答