User Interaction on a UIImageView

2020-06-18 09:11发布

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条回答
再贱就再见
2楼-- · 2020-06-18 10:01

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;

查看更多
登录 后发表回答