UISwipeGestureRecognizer 不工作

发布于 2024-10-07 03:30:29 字数 1736 浏览 4 评论 0原文

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

借用我在 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];

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

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

我假设这是一个有效的gestureRecognizer委托方法,但我在文档中找不到任何对它的引用。

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

我是否将gestureRecognizers 添加到正确的视图中?我尝试将它添加到 UIView、UIScrollView 以及 self.view.superView 中。

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

我做错了什么。

谢谢,

约翰

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.

Borrowing from the sample code I found in SmpleGestureRecognizers, I have put the following code in the viewDidLoad method of the UIScrollView's ViewController...

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

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

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.

Am I adding the gestureRecognizers to the right view? I tried adding it to the UIView, the UIScrollView as well as 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.

What am I doing wrong.

Thanks,

John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚风撩人 2024-10-14 03:30:29

我遇到了同样的问题,我通过使用 UIPanGestureRecognizer 而不是 UISwipeGestureRecognizer 解决了。

为了模拟滑动的检测,我们将在滚动视图中使用手势的速度。
如果x方向的速度>=3000(例如),则将检测到滑动。

如果 x>0 则为右滑动。

我为解决您的情况而实现的代码是:
在名为 _scroll1 的 uiscrollview 中:

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

使用名为 _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];
    }
}

我将其封装在 UIGestureRecognizer 子类中: UISwipe4ScrollGestureRecognizer

I had the same problem and I solved by using UIPanGestureRecognizer instead of 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.

If x>0 it will be a right swipe.

The code I implemented to resolve your situation is:
In a uiscrollview named _scroll1:

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:

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

说谎友 2024-10-14 03:30:29

示例代码和您的代码之间最大的区别是您的代码涉及 UIScrollView

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

The biggest difference between the sample code and your code is that your code involves a UIScrollView.

Internally, scroll views, table views, and web views all use gesture recognizers to some degree. 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.

dawn曙光 2024-10-14 03:30:29

尝试将UIScrollView的delayContentTouches属性设置为NO,也许会有帮助。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文