3

在 viewDidLoad 我设置:

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

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    UISwipeGestureRecognizerDirection temp = recognizer.direction;
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        [self backCalendarPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        [self nextCalendarPressed:nil];
    }
}

recognizer.direction总是等于' 3'。这就是为什么我无法确定它是向左还是向右滑动。

4

5 回答 5

6

如果要区分左右手势,则必须为每个方向设置单独的手势识别器。direction 属性只为您提供您设置为允许的方向(3 = 两个方向)。您可以为两者提供相同的目标方法,并在该方法中询问识别器的方向。

于 2013-06-06T11:32:36.043 回答
0

try this:

The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.

The easiest would be to use two separate gesture recognizers instead.

If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their Simple Gesture Recognizers

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeGestureLeft];

UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRight];



-(void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
    [self backCalendarPressed:nil];
}

-(void)handleSwipeFromRight:(UISwipeGestureRecognizer *)recognizer {
    [self nextCalendarPressed:nil];
}
于 2013-06-06T11:35:38.867 回答
0

基本上,您不能为单个滑动手势识别器设置多个方向。对左右方向使用单独的手势识别器。

于 2013-06-06T11:36:52.147 回答
0

你应该试试这个。它工作正常。

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backCalendarPressed:)];
            [swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft ];
            [self.view addGestureRecognizer:swipeLeft];
            swipeLeft=nil;

            UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextCalendarPressed:)];
            [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
            [self.view addGestureRecognizer:swipeRight];
于 2013-06-06T11:31:48.940 回答
-1

您应该在此处理程序方法中实现所有 UIGestureRecognizer 状态。因此,您可以处理所有情况。我之所以这么说是因为我怀疑手势识别器尚未正确识别它的手势。试着在箱子里寻找方向UIGestureRecognizerStateChanged

于 2013-06-06T11:32:22.837 回答