简体   繁体   English

UISwipeGestureRecognizer不起作用

[英]UISwipeGestureRecognizer not working

I have a UIView inside of a UIScrollView, both created using IB. 我有一个UIScrollView内部的UIView,两者都是使用IB创建的。 The UIView scrolls horizontally inside the UIScrollView. UIView在UIScrollView中水平滚动。 I want to detect left and right 2 finger swipes. 我想检测左右2个手指滑动。

Borrowing from the sample code I found in SmpleGestureRecognizers , I have put the following code in the viewDidLoad method of the UIScrollView's ViewController... 借用我在SmpleGestureRecognizers中找到的示例代码,我将以下代码放入UIScrollView的ViewController的viewDidLoad方法中...

UIGestureRecognizer *recognizer;
UISwipeGestureRecognizer *swipeRightRecognizer, *swipeLeftRecognizer;

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
swipeRightRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeRightRecognizer.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swipeRightRecognizer];
[recognizer release];

recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.numberOfTouchesRequired = 2;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftRecognizer];
[recognizer release];

I have set in the viewcontroller.h. 我已经在viewcontroller.h中设置了。 and have the following delegate method... 并具有以下委托方法...

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
   return YES;
}

I am assuming this is a valid gestureRecognizer delegate method, but I cannot find any reference to it in the documentation. 我假设这是一个有效的gestureRecognizer委托方法,但是我在文档中找不到任何引用。

I do not get any errors but nothing happens when I do a 2 finger swipe. 我没有出现任何错误,但是当我用两根手指滑动时什么也没有发生。 The delegate method is not called and neither is my action method. 没有调用委托方法,也没有调用我的动作方法。 I tried removing the numbeOfTouchesRequired call to see if it might work with a single finger swipe to no avail. 我尝试删除numbeOfTouchesRequired调用以查看是否可以通过单指轻扫而无济于事。

Am I adding the gestureRecognizers to the right view? 我要在正确的视图中添加gestureRecognizers吗? I tried adding it to the UIView, the UIScrollView as well as self.view.superView. 我尝试将其添加到UIView,UIScrollView以及self.view.superView。

The sample code runs great. 示例代码运行良好。 The only difference I can see between my implementation of the code and the sample code is the fact that I used IB to create the views and the sample code did not. 我在代码的实现和示例代码之间看到的唯一区别是,我使用IB创建视图,而示例代码没有创建视图。 I suspect that something is consuming the swipe gesture before it gets to my recognizers. 我怀疑某种东西正在消耗手势手势,然后才能传递给我的识别器。

What am I doing wrong. 我究竟做错了什么。

Thanks, 谢谢,

John 约翰

I had the same problem and I solved by using UIPanGestureRecognizer instead of UISwipeGestureRecognizer. 我遇到了同样的问题,我使用UIPanGestureRecognizer而不是UISwipeGestureRecognizer解决了。

To emulate the detection of swipe, we'll play with the speed of gesture in scrollview. 为了模拟滑动检测,我们将以滚动视图中的手势速度进行播放。 If the speed of x direction >= 3000 (for example) the swipe will be detected. 例如,如果x方向的速度> = 3000,则将检测到滑动。

If x>0 it will be a right swipe. 如果x> 0,将是向右滑动。

The code I implemented to resolve your situation is: In a uiscrollview named _scroll1: 我为解决您的情况而实现的代码是:在名为_scroll1的uiscrollview中:

UIPanGestureRecognizer *pan;
pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(Swipe4ScrollViews:)];
[pan setMinimumNumberOfTouches:2];
[_scroll1 addGestureRecognizer:pan];
[pan release];

With a global BOOL variable named _panning, Swipe4ScrollViews will do the hard job: 使用名为_panning的全局BOOL变量,Swipe4ScrollViews将完成艰巨的任务:

-(void)Swipe4ScrollViews:(UIPanGestureRecognizer *)sender 
{
    if(sender.state == UIGestureRecognizerStateBegan) _panning = NO;

    CGPoint v =[sender velocityInView:_scroll1];

    NSLog(@"%f, %f",v.x,v.y);

    if( (abs(v.x) >= UMBRAL) && !_panning)
    {
        _panning = YES;
        [sender cancelsTouchesInView];

        if(v.x>0) NSLog(@"Right");
        else NSLog(@"Left");

        [self doSomething];
    }
}

I encapsulated it on a UIGestureRecognizer subclass: UISwipe4ScrollGestureRecognizer 我将其封装在UIGestureRecognizer子类上: UISwipe4ScrollGestureRecognizer

The biggest difference between the sample code and your code is that your code involves a UIScrollView . 示例代码和您的代码之间的最大区别是您的代码涉及UIScrollView

Internally, scroll views, table views, and web views all use gesture recognizers to some degree. 在内部,滚动视图,表格视图和Web视图在某种程度上都使用手势识别器。 If you're expecting to receive gestures within those views – gestures that are similar to the ones already supported internally – they will almost certainly be consumed or significantly delayed before you can get to them. 如果您希望这些视图中收到手势(类似于内部已经支持的手势),则几乎可以肯定它们将被消耗或显着延迟,然后才能使用。 Receiving gestures outside or above those views should work fine, if your use case supports it. 如果用例支持,则在这些视图之外之上接收手势应该可以正常工作。

尝试将UIScrollView的delayContentTouches-property设置为NO,也许会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM