通过触摸传递到下面的 UIViews [英] Passing through touches to UIViews underneath

查看:21
本文介绍了通过触摸传递到下面的 UIViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView,上面有 4 个按钮,另一个 UIView 位于按钮视图之上.最顶部的视图包含一个 UIImageView,上面有一个 UITapGestureRecognizer.

I have a UIView with 4 buttons on it and another UIView on top of the buttons view. The top most view contains a UIImageView with a UITapGestureRecognizer on it.

我试图创建的行为是,当用户点击 UIImageView 时,它会在屏幕右下角的小屏幕和动画变大之间切换.当它很大时,我希望底部视图上的按钮被禁用,当它很小并且在右下角时,我希望将触摸传递到按钮并让它们正常工作.我快到了,但除非禁用顶部视图的 UserInteractions,否则我无法触摸到按钮.

The behavoir I am trying to create is that when the user taps the UIImageView it toggles between being small in the bottom right hand corner of the screen and animating to become larger. When it is large I want the buttons on the bottom view to be disabled and when it is small and in the bottom right hand corner I want the touches to be passed through to the buttons and for them to work as normal. I am almost there but I cannot get the touches to pass through to the buttons unless I disable the UserInteractions of the top view.

我在我的 initWithFrame: 顶部视图中有这个:

I have this in my initWithFrame: of the top view:

// Add a gesture recognizer to the image view
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];

我这是我的 imageTapped: 方法:

- (void) imageTapped:(UITapGestureRecognizer *) gestureRecognizer {
    // Toggle between expanding and contracting the image
    if (expanded) {
        [self contractAnimated:YES];
        expanded = NO;
        gestureRecognizer.cancelsTouchesInView = NO;
        self.userInteractionEnabled = NO;
        self.exclusiveTouch = NO;
    }
    else {
        [self expandAnimated:YES];
        expanded = YES;
        gestureRecognizer.cancelsTouchesInView = NO;
        self.userInteractionEnabled = YES;
        self.exclusiveTouch = YES;
    }
}

使用上面的代码,当图像很大时按钮处于非活动状态,当我触摸图像时它会缩小并且按钮变为活动状态.但是,小图像不会受到触摸,因此不会扩展.

With the above code, when the image is large the buttons are inactive, when I touch the image it shrinks and the buttons become active. However, the small image doesn't receive the touches and therefore wont expand.

如果我在这两种情况下都设置了 self.userInteractionEnabled = YES,那么图像会在触摸时扩展和收缩,但按钮永远不会收到触摸并表现为禁用.

If I set self.userInteractionEnabled = YES in both cases, then the image expands and contracts when touched but the buttons never receive touches and act as though disabled.

是否可以在触摸时使图像扩展和收缩,但如果图像处于收缩状态,则下方的按钮只能接收触摸?我是否在这里做了一些愚蠢的事情而遗漏了一些明显的东西?

Is there away to get the image to expand and contract when touched but for the buttons underneath to only receive touches if the image is in its contracted state? Am I doing something stupid here and missing something obvious?

我要让这个工作变得非常疯狂,所以任何帮助都将不胜感激,

I am going absolutely mad trying to get this to work so any help would be appreciated,

戴夫

更新:

为了进一步测试,我覆盖了 touchesBegan:touchesCancelled: 方法,并在包含 UIImageView 的视图上调用了它们的超级实现.使用上面的代码,touchesCancelled: 永远不会被调用,而 touchesBegan: 总是被调用.

For further testing I overrode the touchesBegan: and touchesCancelled: methods and called their super implementations on my view containing the UIImageView. With the code above, the touchesCancelled: is never called and the touchesBegan: is always called.

所以看起来视图正在得到触摸,它们只是没有传递到下面的视图.

So it would appear that the view is getting the touches, they are just not passed to the view underneath.

更新

这是因为响应者链的工作方式吗?我的视图层次结构如下所示:

Is this because of the way the responder chain works? My view hierarchy looks like this:

VC - View1
     -View2
      -imageView1 (has tapGestureRecogniser)
      -imageView2
     -View3
      -button1
      -button2

我认为操作系统首先执行一个 hitTest,因为 View2 在前面,所以应该得到所有的接触,并且这些都不会传递给 View3,除非 userInteractions 为 View2 设置为 NO,在这种情况下,imageView1 也被阻止接收触及.这是它的工作原理吗?有没有办法让 View2 将它的接触传递给 View3?

I think the OS first does a hitTest as says View2 is in front so should get all the touches and these are never passed on to View3 unless userInteractions is set to NO for View2, in which case the imageView1 is also prevented from receiving touches. Is this how it works and is there a way for View2 to pass through it's touches to View3?

推荐答案

UIGestureRecognizer 是我认为的红鲱鱼.最后为了解决这个问题,我覆盖了我的 UIView:

The UIGestureRecognizer is a red herring I think. In the end to solve this I overrode the pointInside:withEvent: method of my UIView:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL pointInside = NO;

    if (CGRectContainsPoint(imageView.frame, point) || expanded) pointInside = YES;

    return pointInside;
}

如果您触摸 imageView 或设置了其扩展标志,这会导致视图捕获所有触摸.如果未展开,则仅捕获在 imageView 上的触摸.

This causes the view to trap all touches if you touch either the imageView or if its expanded flag is set. If it is not expanded then only trap the touches if they are on the imageView.

通过返回 NO,顶级 VC 的 View 查询其视图层次结构的其余部分以寻找命中.

By returning NO, the top level VC's View queries the rest of its view hierarchy looking for a hit.

这篇关于通过触摸传递到下面的 UIViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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