IOS Touch and gestures

Source: Internet
Author: User

Touch

Responder Object

Not all objects in iOS can handle events, and only objects that inherit Uiresponder can receive and handle events. We call it "Responder object".

Uiapplication,uiviewcontroller,uiview are inherited from Uiresponder, so they are all responder objects that are capable of receiving and handling events.

UIView?????? Three situations where touch time is not accepted??????????????????

    • • ???? User interaction is not accepted???????????? userinteractionenable = NO;?? (Example: Uilabel and Uiimageview???? These controls userinteractionenable default to?????? No so these controls are not accepted by default for touch events)

    • • ?? Hide:?? Hidden=yes;

    • • Transparent:???? alpha=0.0~0.01

      ???? The way to respond to touch can be achieved by uiresponder the following four methods

      -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event;????????????????

      This method is triggered when the user starts to touch

      -(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event;???????????????????? This method is triggered when the user's finger starts to move

      -(void) touchesended: (Nsset *) touches withevent: (Uievent *) event;?????????? This method is triggered when the user touches the end

      -(void) touchescancelled: (Nsset *) touches withevent: (uievent*) event;?????????????????????????????? Fires this method when a system event (such as insufficient memory, incoming calls) terminates a touch event

      This method is triggered when the user starts to touch-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event{Nsarray *to        Ucharr = [touches allobjects];//gets the number of fingers when the user starts to touch uitouch *touch = [Toucharr objectatindex:0]; Cgpoint point = [touch Locationinview:[touch view]; Gets the position of the finger at the beginning of the touch//NSLog (@ "%d", touch.tapcount);//Gets the number of clicks of the finger in a short time if (Touch.tapcount = = 1) {[Self perform        Selector: @selector (Tapone) Withobject:nil afterdelay:0.5]; Delay 0.5 seconds call method Tapone} if (Touch.tapcount = = 2) {//If the user wants to simply respond to a double-click, destroy the deferred call [NSObject Cancelpreviousp    Erformrequestswithtarget:self selector: @selector (Tapone) Object:nil]; }}-(void) tapone{NSLog (@ "click once");} This method is triggered when the user's finger starts to move-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event{NSLog (@ " Finger movement ");} This method is triggered when the user touches the end-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event{NSLog (@ " Touch gesture End ");} Fires the method when a system event (such as insufficient memory, incoming calls) terminates a touch event-(void)Touchescancelled: (Nullable nsset<uitouch *> *) touches withevent: (Nullable uievent *) event{} 

      gestures

      UITapGestureRecognizer Click

      label = [[UILabel alloc]initwithframe:cgrectmake (self.view.bounds.size.width/2-50, self.view.bounds.size.height/ 2-50, (+)];    Label.backgroundcolor = [Uicolor greencolor];    label.userinteractionenabled = YES;    [Self.view Addsubview:label];        UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector ( Tapgesturehandle:)];    tapgesture.numberoftapsrequired = 1; Number of clicks required    tapgesture.numberoftouchesrequired = 1;//requires 1 fingers to click    [Label Addgesturerecognizer:tapgesture];

      -(void) Tapgesturehandle: (UITapGestureRecognizer *) gesture{    NSLog (@ "click Trigger");    Cgpoint point = [gesture LocationInView:self.view]; Get click coordinates//}

      Uipinchgesturerecognizer kneading

         Uipinchgesturerecognizer *pinchgesture = [[Uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector ( Pinchgesturehandle:)];    label.multipletouchenabled = YES; Allow multi-touch support    [Label Addgesturerecognizer:pinchgesture];    

      -(void) Pinchgesturehandle: (Uipinchgesturerecognizer *) gesture{    cgfloat scale = Gesture.scale;//get user pinch ratio    CGFloat velocity = gesture.velocity;  Get user pinch speed    label.transform = Cgaffinetransformscale (label.transform, scale, scale);    Gesture.scale = 1.0;    }

      Uipangesturerecognizer Drag and drop

         Uipangesturerecognizer *pangesture = [[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector ( Pangesturehandle:)];    Pangesture.maximumnumberoftouches = 1; Allow the pack processor to support up to a few fingers drag    pangesture.minimumnumberoftouches = 1;//allows the pack-up processor to support a minimum of several finger drag    [Label Addgesturerecognizer:pangesture];

      -(void) Pangesturehandle: (Uipangesturerecognizer *) gesture{    cgpoint point = [gesture Translationinview:label];// Get control Displacement    label.transform = Cgaffinetransformtranslate (Label.transform, Point.x, point.y);    [Gesture settranslation:cgpointmake (0, 0) Inview:label];    }

      Uiswipegesturerecognizer Swipe

      Because a control nail once swipe gesture only supports swiping in one direction, if you need a swipe gesture in four directions, you need to add four times

      for (int i = 0; i < 4; i + +) {        Uiswipegesturerecognizer *swipgesture = [[Uiswipegesturerecognizer Alloc]initwithtar Get:self Action: @selector (Swipgesturehandle:)];        swipgesture.numberoftouchesrequired = 1;        Swipgesture.direction = 1 << i;        [Label addgesturerecognizer:swipgesture];    }

      -(void) Swipgesturehandle: (Uiswipegesturerecognizer *) gesture{    if (gesture.direction = = Uiswipegesturerecognizerdirectionright) {        NSLog (@ "swipe right");    }    if (gesture.direction = = uiswipegesturerecognizerdirectionleft) {        NSLog (@ "swipe left");    }    if (gesture.direction = = Uiswipegesturerecognizerdirectiondown) {        NSLog (@ "swipe down");    }    if (gesture.direction = = uiswipegesturerecognizerdirectionup) {        NSLog (@ "swipe up");}    }

      Uilongpressgesturerecognizer Long Press

          Uilongpressgesturerecognizer *longpress=[[uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (handlelongpress:)];    Longpress.minimumpressduration=1;  At least how many seconds to press will trigger gesture    longpress.allowablemovement=1;  The maximum distance allowed for a finger to move    longpress.numberoftouchesrequired=1;//allows the user to trigger the gesture with several fingers        [_viewtext addgesturerecognizer: Longpress];
      to display a custom menu item by using a long press gesture
          UIMenuItem *transmitem = [[UIMenuItem alloc]initwithtitle:@ ' forward ' action: @selector (Transmitem:)];    UIMenuItem *deleteitem = [[uimenuitem alloc]initwithtitle:@ ' delete ' action: @selector (DeleteItem:)];    UIMenuItem *reportitem = [[UIMenuItem alloc]initwithtitle:@ "Report" Action: @selector (ReportItem:)];

      -(void) Handlelongpress: (Uilongpressgesturerecognizer *) gesture{    if (gesture.state = = Uigesturerecognizerstatebegan) {        NSLog (@ "long press gesture");        [Self becomefirstresponder];        Uimenucontroller *menu=[uimenucontroller Sharedmenucontroller];        [Email protected] [Transmitem,deleteitem,reportitem];        CGRect Rect=cgrectmake (Ten, ten, +);        [Menu Settargetrect:rect inview:_viewtext];        [Menu Setmenuvisible:yes Animated:yes];    }    } -(BOOL) canbecomefirstresponder{    return YES; -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{    if (action = = @selector (transmitem:) | | Action = = @selector (deleteItem:) | | Action = = @selector (reportitem:))    {return YES;} else{        return NO;}}
      -(void) Transmitem: (ID) sender{    NSLog (@ "forwarding");} -(void) DeleteItem: (ID) sender{    NSLog (@ "delete");} -(void) ReportItem: (ID) sender{    NSLog (@ "forwarding");}
if the control supports two gestures at a time, you need to import
        #import <UIKit/UIGestureRecognizerSubclass.h>
also, rewrite two methods

Whether to block another gesture-(BOOL) Canbepreventedbygesturerecognizer: (Uigesturerecognizer *) geature{    return NO;} -(BOOL) Canpreventgesturerecognizer: (Uigesturerecognizer *) preventedgesturerecognizer{    return NO;

IOS Touch and gestures

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.