xcode UISwipeGestureRecognizer 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4407171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:59:30  来源:igfitidea点击:

UISwipeGestureRecognizer not working

iphonexcodeuigesturerecognizerswipe

提问by user278859

I have a UIView inside of a UIScrollView, both created using IB. The UIView scrolls horizontally inside the UIScrollView. I want to detect left and right 2 finger swipes.

我在 UIScrollView 中有一个 UIView,两者都是使用 IB 创建的。UIView 在 UIScrollView 内水平滚动。我想检测左右两根手指的滑动。

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. and have the following delegate method...

我已经在viewcontroller.h 中设置了。并有以下委托方法......

- (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.

我没有收到任何错误,但是当我用 2 指滑动时没有任何反应。没有调用委托方法,也没有调用我的操作方法。我尝试删除 numbeOfTouchesRequired 调用以查看它是否可以通过单指滑动而无济于事。

Am I adding the gestureRecognizers to the right view? I tried adding it to the UIView, the UIScrollView as well as self.view.superView.

我是否将gestureRecognizers 添加到正确的视图中?我尝试将它添加到 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. I suspect that something is consuming the swipe gesture before it gets to my recognizers.

示例代码运行良好。我在代码的实现和示例代码之间看到的唯一区别是我使用 IB 创建视图而示例代码没有。我怀疑有什么东西在到达我的识别器之前消耗了滑动手势。

What am I doing wrong.

我究竟做错了什么。

Thanks,

谢谢,

John

约翰

回答by Joze

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

回答by Justin Spahr-Summers

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. If you're expecting to receive gestures withinthose 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 outsideor abovethose views should work fine, if your use case supports it.

在内部,滚动视图、表格视图和 Web 视图都在某种程度上使用了手势识别器。如果您希望这些视图中接收手势——与内部已经支持的手势类似的手势——它们几乎肯定会在您到达它们之前被消耗或显着延迟。如果您的用例支持,这些视图之外之上接收手势应该可以正常工作。

回答by thgc

Try setting the delayContentTouches-property of the UIScrollView to NO, maybe it'll help.

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