Detect if certain UIView was touched amongst other UIViews

后端 未结 8 1618
迷失自我
迷失自我 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:09

    In touchesBegan check that the touched view is the one you want, solved the problem for me when I tried to identified if the user touched a specific ImageView.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        if ([touch view] ==  imageView ) {
            ... your code to handle the touch
        }
        ...
    

    If you adjust the content for the view diamensions make sure to change the view size accordingly otherwise it will recognize touch event even in area where there in so content in the view. In my case when I tried to keep the image proportion with UIViewContentModeScaleAspectFit although the image was adjusted as required in the "white spaces" areas touch event were catched as well.

提交回复
热议问题