Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Interaction on a UIImageView

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

like image 807
some_id Avatar asked Nov 01 '10 22:11

some_id


1 Answers

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;

like image 50
Ole Begemann Avatar answered Sep 25 '22 09:09

Ole Begemann