3

我正在编写代码以在视图上向上或向下移动我的两根手指以更改某些状态。代码如下:

UISwipeGestureRecognizer *aSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
aSwipeGesture.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGesture];


- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {
    NSLog(@"Swipe received.");
    if (sender.direction==UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"swipe up");
    } else if (sender.direction==UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"swipe down");
    }
}

然而,我能收到的唯一打印日志是 Swipe 收到,如下所示。我无法收到向上或向下滑动的消息,我错过了什么吗?谢谢

ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.
ViewController.m:228    Swipe received.

更新:我必须用两根手指来完成滑动动作。

4

5 回答 5

6

试试这个

UISwipeGestureRecognizer *aSwipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp];
aSwipeGestureUp.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureUp];

UISwipeGestureRecognizer *aSwipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
[aSwipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
aSwipeGestureDown.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureDown];
于 2013-04-24T06:27:08.557 回答
5

遗憾的是,API 完全没有意义,您可以设置一个 UISwipeGestureRecognizer 来识别 4 个方向中的任何一个上的滑动,但它不会告诉您滑动发生的方向。

他们足够小心,允许一起使用 4 个常量(它是一个位图),但 API 只是坏了。

您将不得不为每个方向使用一个识别器,真可惜!

于 2013-09-17T17:04:16.630 回答
2

在您的情况下,您已将滑动方向设置为 UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown 。因此,在委托回调中,您将收到与 sender.direction 相同的值,这就是为什么不打印日志的原因,因为枚举值与 sender.direction 值不匹配。

如果您想分别处理向上和向下滑动,您需要创建 2 个带有向上和向下滑动方向的滑动手势并添加到您的视图中。然后根据发送者的方向做任务。

于 2013-04-24T06:28:35.040 回答
2

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
}
于 2014-09-22T12:25:22.637 回答
-3

删除 aSwipeGesture.numberOfTouchesRequired = 2; 并尝试

于 2013-04-24T06:09:31.280 回答