我在手势识别器响应不正确的子视图时遇到问题,怎么办?

发布于 2024-11-09 12:01:58 字数 1470 浏览 2 评论 0原文

我有一个名为 ImageViewController 的父视图。在此视图中,我添加了两个名为 ImageDetailViewController 的子视图。在此详细视图控制器中,我声明了一些手势,这些手势仅需要在各自的视图上执行某些操作。代码如下:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeModeButtonPushed:)];
[tap setNumberOfTapsRequired:2];
[tap setNumberOfTouchesRequired:1];
[tap setDelegate:self];
[openGLView addGestureRecognizer:tap];
[tap release];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchDidDrag:)];
[panRecognizer setMaximumNumberOfTouches:1];
[openGLView addGestureRecognizer:panRecognizer];
[panRecognizer release];

UIPanGestureRecognizer *shiftRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(shiftView:)];
[shiftRecognizer setMinimumNumberOfTouches:2];
[openGLView addGestureRecognizer:shiftRecognizer];
[shiftRecognizer release];

UIPinchGestureRecognizer *pinchRecog = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(viewPinched:)];
[openGLView addGestureRecognizer:pinchRecog];
[pinchRecog release];

因此,我需要在该父视图内并排加载两个详细视图,然后我需要能够在一个视图或另一个视图上执行这些手势,并让该视图做出响应。

问题是,当我用一根手指平移时,有时它会调用它应该调用的视图,但大多数情况下,它会在另一个视图上触发!这没有任何意义!我已经尝试过调试和我能想到的所有其他方法,但我只是看不出两个单独声明的对象如何像这样混合在一起。

更奇怪的是,有些手势确实可以正常工作。所以只是这个一根手指平移的手势就把一切搞砸了。

我可能会错过什么?我怎样才能调试这个?一切看起来都像是对象确实是分开的,但它们只是没有那样表现......

I have a parent view called ImageViewController. To this view I add up to two subviews called ImageDetailViewController. In this detail view controller I declare a few gestures that need to do certain things on their respective views only. Here is the code:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeModeButtonPushed:)];
[tap setNumberOfTapsRequired:2];
[tap setNumberOfTouchesRequired:1];
[tap setDelegate:self];
[openGLView addGestureRecognizer:tap];
[tap release];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchDidDrag:)];
[panRecognizer setMaximumNumberOfTouches:1];
[openGLView addGestureRecognizer:panRecognizer];
[panRecognizer release];

UIPanGestureRecognizer *shiftRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(shiftView:)];
[shiftRecognizer setMinimumNumberOfTouches:2];
[openGLView addGestureRecognizer:shiftRecognizer];
[shiftRecognizer release];

UIPinchGestureRecognizer *pinchRecog = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(viewPinched:)];
[openGLView addGestureRecognizer:pinchRecog];
[pinchRecog release];

So I need to have two of these detail views load up next to each other inside of this parent view, and then I need to be able to do these gestures on one view or the other, and have that view respond.

The problem is that when I 1 finger pan around with 1 finger, sometimes it will call on the view it is supposed to, but most often, it fires on the other view! It doesn't make any sense! I have tried debugging and everything else I can think of, but I just can't see how two separately declared objects can get mixed up like this.

To make things stranger, there are some gestures that do work correctly. So it's only this one finger pan around gesture that screws everything up.

What could I be missing? How can I even debug this? Everything is looking like the objects are indeed separate, but they just aren't behaving that way...

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

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

发布评论

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

评论(1

ぶ宁プ宁ぶ 2024-11-16 12:01:58

您可以通过找出手势识别器正在调用的视图来调试/解决此问题:看看这段代码:

(void)NAMEOFFUNCTION:(UITapGestureRecognizer*)recognizer {

if ([yourViewController.view pointInside:[recognizer locationInView:presentationSlideViewController.view] withEvent:nil]) {


}

过滤所有子视图,在本例中我正在检查查看手指触摸是否低于定义的 x 坐标和 y 坐标。首先检查该点是否在 _buttonVIew 子视图内://

if ([_buttonView pointInside:[recognizer locationInView:_buttonView] withEvent:nil]) {

然后检查触摸是否在低于 50 的坐标

    if (([recognizer locationInView:_buttonView].x < 50) && ([_leftArrow isEnabled])) {
        pageIndex -= 1;
    }
   //otheriwse check to see if its over 970
        else if (([recognizer locationInView:_buttonView].x > 970) && ([_rightArrow isEnabled])) {
            pageIndex += 1;
        }

    [self scrollViewScrollToRect:pageIndex];
    [self fadeArrows:1.0];
    return;

    }

}

You could debug/solve this by finding out what view your gesture recogniser is calling on: Have a look at this code:

(void)NAMEOFFUNCTION:(UITapGestureRecognizer*)recognizer {

if ([yourViewController.view pointInside:[recognizer locationInView:presentationSlideViewController.view] withEvent:nil]) {


}

filter through all your subviews, in this example i'm checking to see if the finger touch is below a defined x and then y coordinate. First check to see if the point is inside the _buttonVIew subview://

if ([_buttonView pointInside:[recognizer locationInView:_buttonView] withEvent:nil]) {

Then check to see if the touch is in a coordinate lower than 50

    if (([recognizer locationInView:_buttonView].x < 50) && ([_leftArrow isEnabled])) {
        pageIndex -= 1;
    }
   //otheriwse check to see if its over 970
        else if (([recognizer locationInView:_buttonView].x > 970) && ([_rightArrow isEnabled])) {
            pageIndex += 1;
        }

    [self scrollViewScrollToRect:pageIndex];
    [self fadeArrows:1.0];
    return;

    }

}

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