-( Void ) Touchesbegan :( nsset *) touches withevent :( uievent *) Event {
Uitouch * Touch = [touches anyobject];
Gesturestartpoint = [Touch locationinview: Self. View];
}
-( Void ) Touchesmoved :( nsset *) touches withevent :( uievent *) Event {
Uitouch * Touch = [touches anyobject];
Cgpoint currentposition = [Touch locationinview: Self. View];
Cgfloat deltax = fabsf (gesturestartpoint. X-currentposition. X );
Cgfloat deltay = fabsf (gesturestartpoint. Y-currentposition. y );
If (Deltax> = kminimumgesturelength & deltay <= kmaximumvariance ){
Label. Text = @" Horizontal swipe Detected " ;
[Self defined mselector: @ selector (erasetext)
Withobject: Nil afterdelay: 2 ];
} Else If (Deltay> = kminimumgesturelength &&
Deltax <= kmaximumvariance ){
Label. Text = @" Vertical swipe Detected " ;
[Self defined mselector: @ selector (erasetext) withobject: Nil
Afterdelay: 2 ];
}
}
You can also use automatic Gesture Recognition :(Uigesturerecognizer)
-(Void) Viewdidload
{
[Super viewdidload];
Uiswipegesturerecognizer * vertical = [uiswipegesturerecognizer alloc]
Initwithtarget: Self action: @ selector (reportverticalswipe :)];
Vertical. Direction = uiswipegesturerecognizerdirectionup |
Uiswipegesturerecognizerdirectiondown;
[Self. View addgesturerecognizer: vertical];
Uiswipegesturerecognizer * horizontal = [uiswipegesturerecognizer alloc]
Initwithtarget: Self action: @ selector (reporthorizontalswipe :)];
Horizontal. Direction = uiswipegesturerecognizerdireleft left |
Uiswipegesturerecognizerdirectionright;
[Self. View addgesturerecognizer: horizontal];
}
Then add two custom response methods:
-(Void) Reporthorizontalswipe :( uigesturerecognizer *) recognizer {
Label. Text =@"Horizontal swipe Detected";
[Self defined mselector: @ selector (erasetext) withobject: Nil afterdelay:2];
}
-(Void) Reportverticalswipe :( uigesturerecognizer *) recognizer {
Label. Text =@"Vertical swipe Detected";
[Self defined mselector: @ selector (erasetext) withobject: Nil afterdelay:2];
}
OK!
The above is only a single light sweep action, the following is the situation where multiple light sweep actions are performed simultaneously:
Add a loop in viewdidload:
-(Void ) Viewdidload
{
[Super viewdidload];
For (Nsuinteger touchcount = 1 ; Touchcount <= 5 ; Touchcount ++ ){
Uiswipegesturerecognizer * vertical;
Vertical = [[uiswipegesturerecognizer alloc] initwithtarget: Self
Action: @ selector (reportverticalswipe :)];
Vertical. Direction = uiswipegesturerecognizerdirectionup |
Uiswipegesturerecognizerdirectiondown;
Vertical. numberoftouchesrequired = touchcount;
[Self. View addgesturerecognizer: vertical];
Uiswipegesturerecognizer * horizontal;
Horizontal = [[uiswipegesturerecognizer alloc] initwithtarget: Self
Action: @ selector (reporthorizontalswipe :)];
Horizontal. Direction = uiswipegesturerecognizerdireleft left |
Uiswipegesturerecognizerdirectionright;
Horizontal. numberoftouchesrequired = touchcount;
[Self. View addgesturerecognizer: horizontal];
}
}
Modify the Response Action function:
-(Void ) Reporthorizontalswipe :( uigesturerecognizer *) recognizer {
Label. Text = [nsstring stringwithformat: @" % @ Horizontal swipe Detected " ,
[Self descriptionfortouchcount: [recognizer numberoftouches];
[Self defined mselector: @ selector (erasetext) withobject: Nil afterdelay: 2 ];
}
-( Void ) Reportverticalswipe :( uigesturerecognizer *) recognizer {
Label. Text = [nsstring stringwithformat:@" % @ Vertical swipe Detected " ,
[Self descriptionfortouchcount: [recognizer numberoftouches];
[Self defined mselector: @ selector (erasetext) withobject: Nil afterdelay: 2 ];
}
-(Nsstring *) descriptionfortouchcount :( nsuinteger) touchcount {
Switch (Touchcount ){
Case 2 :
Return @" Double " ;
Case 3 :
Return @" Triple " ;
Case 4 :
Return @" Quadruple " ;
Case 5 :
Return @" Quintuple " ;
Default :
Return @"" ;
}
}