Because UIView or Uiviewcontroller are both inherited and Uiresponder, there are uitouch this event. When the user taps the screen, a touch event is generated.
Through the Uitouch event, you can hear the start touch, touch move process, touch end and touch interrupt four different stages of the state, in these methods, we can get a lot of useful information, such as touch point coordinates, touch Hand index, the number of touches, and so on, below a small example to illustrate.
The detailed code is as follows:
/*Defining Properties*/@interfaceViewcontroller () {cgpoint _startpoint;//point at which to start clickingCgpoint _endpoint;//end point of ClickUILabel*_label1;//label showing the status of the current touchUILabel *_label2; UILabel*_label3; UILabel*_label4; Uiimageview*_imageview;//smiley face Picture}/*the series method of touch event Uitouch is as follows < a > to < four >*/#pragmaMark < a > when one or more fingers touch the screen, send touchesbegan:withevent: Message-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{_label1.text=@"Touch Start"; //1. First get the touch screen fingerUitouch * Touch =[touches anyobject]; //2. Click the coordinates of the current pointCgpoint point =[Touch LocationInView:self.view]; _label2.text= [NSString stringWithFormat:@"current Point coordinates: x=%.1f, y=%.1f", Point.x,point.y]; //4. Get the number of times you touch the screen intTapcount =Touch.tapcount; //5. Get the number of finger roots on the touch screen intFingercount =Touches.count; _label3.text= [NSString stringWithFormat:@"Touch screen count is%i, Touch Hand index is%i", Tapcount,fingercount]; //6. The current view is only supported by default single touch if you want to add multi-touch, you must turn on multi-touch modeself.view.multipleTouchEnabled =YES; //7.1. Get the point to start clicking, get the point of the last click, calculate and see what you've done_startpoint =[Touch LocationInView:self.view]; _label4.text=@"";}#pragmaMark < two > when one or more fingers move on the screen, send touchesmoved:withevent: Message-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event{_label1.text=@"Touch Move ..."; Cgpoint Point=[[Touches Anyobject] locationInView:self.view]; _label2.text= [NSString stringWithFormat:@"current Point coordinates: x=%.1f, y=%.1f", Point.x,point.y];}#pragmaMark < three > when one or more fingers leave the screen, send touchesended:withevent: Message-(void) touchesended: (Nsset *) touches withevent: (Uievent *)Event{_label1.text=@"Touch End"; Cgpoint Point=[[Touches Anyobject] locationInView:self.view]; //3. Determine if the image is in the range if(Cgrectcontainspoint (_imageview.frame, point)) {_label2.text=@"stay within the smiley face picture range"; } Else{_label2.text=@"stay outside the smiley face picture"; } //7.2 Calculate start to end offset floatDistancex = FABSF (Point.x-_startpoint.x); //gets the offset of the finger's longitudinal movement floatDistancey = FABSF (Point.y-_startpoint.y); _label4.text= [NSString stringWithFormat:@"x Offset the%.1f,y direction offset the%.1f", Distancex,distancey]; _startpoint=Cgpointzero;}#pragmaMark < four > send touchescancelled:withevent: Message-(void) touchescancelled: (Nsset *) when the touch sequence is unexpectedly canceled by a system event interrupt such as a call-in. Touches withevent: (Uievent *) event{_label1.text=@"Touch Cancel";}
Application of Touch Event Uitouch