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

查看:107
本文介绍了通过触摸到下面的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,

Dave

更新:

进一步测试我覆盖了 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位于前面,因此应该获得所有触摸,除非将ViewInteractions设置为NO,否则这些内容永远不会传递给View3,在这种情况下,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的 pointInside:withEvent:方法:

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天全站免登陆