Detect if certain UIView was touched amongst other UIViews

后端 未结 8 1626
迷失自我
迷失自我 2020-11-30 02:30

I have 3 UIViews, layered on top of one large uiview. I want to know if the user touches the top one and not care about the other ones. I will have a couple of buttons in th

8条回答
  •  庸人自扰
    2020-11-30 03:12

    This can also be done the following way.

    First find the location of the touch in the view you care about:

    CGPoint location = [touches.anyObject locationInView:viewYouCareAbout];
    

    Then see if the point is within the bounds of the view:

    BOOL withinBounds = CGRectContainsPoint(viewYouCareAbout.bounds, location);
    

    Then if it's within bounds, perform your action.

    This can be done with one statement, which for example, can be used directly within an if statement:

    CGRectContainsPoint(viewYouCareAbout.bounds, [touches.anyObject locationInView:viewYouCareAbout])
    

提交回复
热议问题