开发者

User Interaction on a UIImageView

开发者 https://www.devze.com 2023-01-22 19:16 出处:网络
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 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开发者_JS百科 than IB?

Thanks


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;

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号