手势问题:UISwipeGestureRecognizer + UISlider [英] Gesture problem: UISwipeGestureRecognizer + UISlider

查看:194
本文介绍了手势问题:UISwipeGestureRecognizer + UISlider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个手势相关的问题。我实现了UISwipeGestureRecognizer获取滑动左和右的事件,这是正常工作。但是我遇到的问题是UISlider在同一个视图中并不是很好玩。滑块的滑动动作被误认为向左/向右滑动。



以前有人遇到过这个问题,有什么想法可以纠正吗?



非常感谢。



这是视图控制器中包含的代码:

   - (void)viewDidLoad {

[super viewDidLoad];

//设置LEFT和RIGHT刷卡处理
UISwipeGestureRecognizer *识别器;

recognitionizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom :)];
[识别器setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognitionizer];
[识别器发布];

recognitionizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom :)];
[识别器setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognitionizer];
[识别器发布];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)识别器{

if(recognitionizer.direction == UISwipeGestureRecognizerDirectionRight){
NSLog 滑动右);
//做东西
}

如果(recognitionizer.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@Swipe Left);
//做东西
}
}


解决方案

最简单的处理方法可能是为了防止手势识别器在滑块上看到触摸。您可以通过将自己设置为委托,然后实现

   - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch :(UITouch *)touch {
if([touch.view isKindOfClass:[UISlider class]]){
//阻止识别滑块上的触摸
return NO;
}
return YES;
}

如果这不起作用,滑块实际上可能会接收到子视图触摸,所以你可以走上 superview 链,一路测试每个视图。


Got a gesture related problem. I implemented UISwipeGestureRecognizer to get swipe left and right events and that is working fine. However the problem I'm facing is that the UISlider's I have in the same view are not playing nice. The sliding motion of the sliders is being mistaken as a swipe left/right.

Any one experienced this problem before, got any ideas how to correct it?

Many thanks.

Here is the code contained within the view controller:

 - (void)viewDidLoad {

            [super viewDidLoad];

                //Setup handling of LEFT and RIGHT swipes
             UISwipeGestureRecognizer *recognizer;

                recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
                [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
                [[self view] addGestureRecognizer:recognizer];
                [recognizer release];

                recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
                [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
                [[self view] addGestureRecognizer:recognizer];
                [recognizer release];
        }

    -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {

      if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
       NSLog(@"Swipe Right");
       //Do stuff
      }

      if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
       NSLog(@"Swipe Left");
       //Do stuff
      }
    }

解决方案

The simplest way to handle this is probably to prevent the gesture recognizer from seeing touches on your slider. You can do that by setting yourself as the delegate, and then implementing

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UISlider class]]) {
        // prevent recognizing touches on the slider
        return NO;
    }
    return YES;
}

If this doesn't work, it's possible the slider actually has subviews that receive the touch, so you could walk up the superview chain, testing each view along the way.

这篇关于手势问题:UISwipeGestureRecognizer + UISlider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆