Objectiv-C: UIImageView, UILabel, and other Click events without interactive controls by default, uiimageviewuilabel

Source: Internet
Author: User

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}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.