Objectiv-C: UIImageView, UILabel, and other Click events without interactive controls by default, uiimageviewuilabel
In the UI of OC, some commonly used controls, such as UIImageView and UILabel, do not interact by default, that is, clicking on the control, double-clicking or sliding operations are ineffective.
The following method perfectly solves the control Interaction Problem:
(Taking UIImageView as an example, other controls are similar)
First, create a UIImageView:
UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (0, 0, self. view. bounds. size. width, self. view. bounds. size. height)]; // set the imageView image [imageView setImage: [UIImage imageNamed: @ "first.jpg"]; // set the userInteractionEnabled of imageView to YES to enable interactive imageView. userInteractionEnabled = YES;
Then, create a gesture. Here we use the click gesture as an example:
// Create a click gesture tap and specify the processing method as SingleClickUITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (SingleClick :)];
Step 3: Add a gesture to the imageView
[imageView addGestureRecognizer:tap];
Finally, the implementation click method SingleClick
-(Void) SingleClick :( UITapGestureRecognizer *) tap {// obtain the clicked View UIImageView * imageView = (UIImageView *) [tap view] through the gesture tap; // you can operate on the clicked View. // Note: Make sure that the View is correct when the type is forced to convert. What controls are added with the gesture tap, and what type is the transformation}