检测某些UIView是否被其他UIViews触及

Rud*_*ger 31 iphone uiview uitouch

我有3个UIViews,分层在一个大的uiview之上.我想知道用户是否接触到最顶层而不关心其他用户.我将在第二个UIView中有几个按钮,在第三个UIView中有一个UITable.

问题是我在第一个视图上打开userInteractionEngabled并且它可以工作,但是即使我将其关闭,所有其他视图也以相同的方式响应.如果我在self.view上禁用userInteractionEnabled,则它们都不会响应.我也无法检测touchesBegan委托方法中触摸了哪个视图.

我的代码:

UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aView = userInteractionEnabled = YES;
[self.view addSubview:aView];

UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 50)];
bView.userInteractionEnabled = NO;
[self.view addSubview:bView];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//This gets called for a touch anywhere
}
Run Code Online (Sandbox Code Playgroud)

Paw*_*wel 62

为了检查是否触摸了另一个视图中的某个视图,您可以使用hitTest.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
Run Code Online (Sandbox Code Playgroud)

在touchesBegan的自定义实现中,检查触摸设置中的每个触摸.hitTest方法的点可以使用获得

- (CGPoint)locationInView:(UIView *)view;
Run Code Online (Sandbox Code Playgroud)

方法,其中视图是您的superView(包含其他视图的视图).

编辑:这是一个快速的自定义实现:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint locationPoint = [[touches anyObject] locationInView:self];
    UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

保罗,我希望这很有帮助

  • 应该注意的是,使用参数`nil`调用`locationInView:`会在主窗口的坐标空间中给出位置`CGPoint`. (2认同)

小智 27

在我的情况下,我想找到的UIView没有被hitTest返回.但是下面的代码工作得更好:

    CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
    CGPoint viewPoint = [myImage convertPoint:locationPoint fromView:self.view];
    if ([myImage pointInside:viewPoint withEvent:event]) {
       do something
    }
Run Code Online (Sandbox Code Playgroud)


Ben*_*ron 14

这也可以通过以下方式完成.

首先在您关注的视图中找到触摸的位置:

CGPoint location = [touches.anyObject locationInView:viewYouCareAbout];
Run Code Online (Sandbox Code Playgroud)

然后查看该点是否在视图的范围内:

BOOL withinBounds = CGRectContainsPoint(viewYouCareAbout.bounds, location);
Run Code Online (Sandbox Code Playgroud)

然后,如果它在界限范围内,请执行您的操作.

这可以通过一个语句完成,例如,可以直接在if语句中使用:

CGRectContainsPoint(viewYouCareAbout.bounds, [touches.anyObject locationInView:viewYouCareAbout])
Run Code Online (Sandbox Code Playgroud)


Kar*_*yan 13

SWIFT代码:

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            if touch.view == self.view {
                self.hideInView()
            } else {
                return
            }

        }
    }
Run Code Online (Sandbox Code Playgroud)


Pic*_*chi 8

touchesBegan检查触摸的视图是你想要的,当我试图确定用户是否触摸了特定的ImageView时,我解决了问题.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if ([touch view] ==  imageView ) {
        ... your code to handle the touch
    }
    ...
Run Code Online (Sandbox Code Playgroud)

如果您调整视图直径的内容,请确保相应地更改视图大小,否则即使在视图中具有此类内容的区域中,它也将识别触摸事件.在我的情况下,当我尝试使用UIViewContentModeScaleAspectFit保持图像比例,尽管图像在"白色空间"区域中根据需要进行了调整,但也触摸了事件.


Ker*_*dex 7

我的快速版本检查触摸发生在 infoView 中:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first, infoView.bounds.contains(touch.location(in: infoView))  else { return }
    print("touchesBegan") // Replace by your own code
} // touchesBegan
Run Code Online (Sandbox Code Playgroud)