UIView:触动:刷卡?

时间:2011-07-28 22:44:06

标签: objective-c cocoa-touch

有没有更简单的方法来检测滑动动作及其方向而不进行touchesBegin和touchesEnd?

感谢

2 个答案:

答案 0 :(得分:4)

如何使用UISwipeGestureRecognizer?您创建了UISwipeGestureRecognizer,然后将其添加到视图中,请查看文档UISwipeGestureRecognizer

设置可能如下所示:

UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
[myView addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release]; swipeRecognizer = nil;

然后实现您在滑动时调用的方法(这是@selector(swiped:)

- (void)swiped:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Direction = right");
    } else if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Direction = left");
    }
}

答案 1 :(得分:0)

这只是一个方向。你应该像这样使用2个方向。

    UISwipeGestureRecognizer *swipeRecognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
swipeRecognizerLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[_cameraView addGestureRecognizer:swipeRecognizerLeft];

UISwipeGestureRecognizer *swipeRecognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiped:)];
swipeRecognizerRight.direction = UISwipeGestureRecognizerDirectionRight;
[_cameraView addGestureRecognizer:swipeRecognizerRight];