IPhone development (8) --- screen touch Detection

Source: Internet
Author: User

 

In this case, you can customize the touch event on the UIView. As an example, you can simply detect the touch event and display the current coordinates on the console.

 

First, add a new file, such:


 

In the displayed dialog box, select the Objective C Class Objective UIView of the Cocoa Touch class.

 

Select Touch from the Add project menu. To detect the touch time, you need to implement the following functions.

 

 

-(Void) touchesBegan :( NSSet *) touches

WithEvent :( UIEvent *) event;

This function is immediately adjusted after you touch the screen. To customize his behavior, we will implement it as follows:

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {

UITouch * touch = [touches anyObject];

CGPoint pt = [touch locationInView: self];

Printf ("point = % lf, % lf \ n", pt. x, pt. y );

}

The code above extracts the coordinates of the touch point and prints it to the console.

 

If you want to obtain the information of multi-touch (not just a finger), you must use the anyObject instance to specify the UIView.

 

In addition, the TouchAppDelegate applicationDidFinishLaunching function is implemented as follows:

 

-(Void) applicationDidFinishLaunching :( UIApplication *) application {

TouchView * view = [[TouchView alloc]

InitWithFrame: CGRectMake (100,100,200,200)];

View. backgroundColor = [UIColor greenColor];

[Window addSubview: view];

[Window makeKeyAndVisible];

[View release];

}

The area of the rectangle specified by intiWithFrame can be used in any way. In addition, to specify the size of the touch area, set view. backgroundColor.

 

Although TouchView instances are created in TouchAppDelegate through initWithFrame, the management responsibility is handed over to window through addSubview: view. That is to say, both TouchAppDelegate and window instances manage TouchView instances. Therefore, [view release] is used to release the management responsibility of TouchAppDelegate.

From: Yi Feiyang

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.