Link: http://www.yifeiyang.net/iphone-developer-advanced-8-touch-screen-test-event/Reprinted text.
IPhone development (8) --- screen touch Detection
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.