Gesture operations in iPhone development: swipes

Source: Internet
Author: User
-( 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 @"" ;
}
}

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.