User Interaction on a UIImageView

房东的猫 提交于 2019-12-21 05:00:29

问题


I have a number of UIImageView which have buttons on top of them.

I would like to enable user interaction on the UIImageView behind these buttons.

I see the option in IB, but would like to know how to trigger some code when the UIImageView is actually touched.

How does one do this and how is it set to enabled and disabled in the code rather than IB?

Thanks


回答1:


how to trigger some code when the UIImageView is actually touched.

You have two options:

  1. Create an instance of UITapGestureRecognizer (or another gesture recognizer), specifying a target and an action method. Then add the gesture recognizer to the image view with -[UIView addGestureRecognizer:]. Works in OS 3.2+.

  2. Subclass UIImageView and override the -touches... methods. Make sure the image views you create are instances of your custom subclass.

See the documentation for details.

how is it set to enabled and disabled in the code rather than IB

Simple: imageView.userInteractionEnabled = YES;



来源:https://stackoverflow.com/questions/4073598/user-interaction-on-a-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!