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