Uigesturerecognizergesture recognizer, the parent of common gestures
You can write code, or you can drag and drop app gestures.
1. Tap Touch
Four methods:
-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{_label1.text = @ "Touch start"; 1. Get the touch screen finger uitouch *touch = [touches anyobject]; 2. Get the coordinates of the position where the current finger is located _startpoint = [Touch LocationInView:self.view]; 3. Display coordinates _label1.text = [NSString stringwithformat:@ "Start: x=%.1f,y=%.1f", _startpoint.x,_startpoint.y]; 5. Get touch screen count int tapcount = Touch.tapcount; 6. Get the number of finger clicks int fingercount = Touches.count; _label4.text = [NSString stringwithformat:@ "touch count:%i, hand Index:%i", Tapcount,fingercount];} -(void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{//As long as there is movement to enter this method _label2.text = @ "Touch move"; 1. Get the touch screen finger uitouch *touch = [touches anyobject]; 2. Get the coordinates of the position where the current finger is located cgpoint point = [Touch LocationInView:self.view]; 3. Display coordinates _label2.text = [NSString stringwithformat:@ "x=%.1f,y=%.1f", Point.x,point.y];} -(void) touchesended: (Nsset *) touches withevent: (uievent *) event{_label3.text = @ "Touch End"; 1. Get the touch screen finger uitouch *touch = [Touches AnyobjeCT]; 2. Get the coordinates of the position where the current finger is located _endpoint = [Touch LocationInView:self.view]; 3. Display coordinates _label3.text = [NSString stringwithformat:@ "x=%.1f,y=%.1f", _endpoint.x,_endpoint.y]; 4. Calculate the displacement FABSF () to calculate the absolute value of float Distancesx = FABSF (_endpoint.x-_startpoint.x); float Distancesy = FABSF (_endpoint.y-_STARTPOINT.Y); _label4.text = [NSString stringwithformat:@ "End: X Direction shift:%.1f,y Direction Shift:%.1f", Distancesx,distancesy]; 7. Determine if the last point is in the picture if (Cgrectcontainspoint (_imageview.frame, _endpoint)) {_label4.text = @ "Lifted in the picture"; }else{_label4.text = @ "Not in the picture when lifted"; }//Accept single Touch event by default, multi-touch need to open self.view.multipleTouchEnabled = YES;} -(void) touchescancelled: (Nsset *) touches withevent: (uievent *) event{_label.text = @ "Touch Cancel";}
2. Cleaning
1. Create a swipe gesture object and add a method for it to perform gestures uiswipegesturerecognizer *swiper = [[Uiswipegesturerecognizer alloc] Initwithtarget:self Action: @selector (swiperightaction:)]; 2. Set the direction of the swipe gesture/* uiswipegesturerecognizerdirectionright = 1 << 0, Uiswipegesturerecognizerdirectionleft = 1 << 1, uiswipegesturerecognizerdirectionup = 1 << 2, Uiswipegesturerecognizerdirectiondown = 1 << 3 */swiper.direction = uiswipegesturerecognizerdirectionright; 3. Add gestures to the view [Self.view Addgesturerecognizer:swiper]; You cannot add multiple swipe directions for a gesture!!! Sweep left Uiswipegesturerecognizer *swipel = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipeleftaction:)]; Swipel.direction = Uiswipegesturerecognizerdirectionleft; [Self.view Addgesturerecognizer:swipel]; Sweep up Uiswipegesturerecognizer *swipeu = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipeupaction:)]; Swipeu.direction = UiswipegestuRerecognizerdirectionup; [Self.view Addgesturerecognizer:swipeu]; Sweep down Uiswipegesturerecognizer *swiped = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector ( Swipedownaction:)]; Swiped.direction = Uiswipegesturerecognizerdirectiondown; [Self.view addgesturerecognizer:swiped];
3. Click
1. Create a click gesture UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Singleaction:)]; 2.1 Set the hit hand index [Singletap setnumberoftouchesrequired:1]; 2.2 Set the number of clicks [Singletap setnumberoftapsrequired:1]; 3. Add the finger to the view [Self.view Addgesturerecognizer:singletap]; One finger double-click uitapgesturerecognizer *doubletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Doubleaction:)]; [Doubletap setnumberoftouchesrequired:1]; [Doubletap Setnumberoftapsrequired:2]; [Self.view Addgesturerecognizer:doubletap]; Double-click can respond to double-click time, but also be considered as two clicks event, so you need to set the priority of the gesture [Singletap Requiregesturerecognizertofail:doubletap]; Click Priority level below double click//two finger double click event UITapGestureRecognizer *doubletapeve = [[UITapGestureRecognizer alloc] Initwith Target:self Action: @selector (doubletapeveaction:)]; [Doubletapeve Setnumberoftouchesrequired:2]; [Doubletapeve Setnumberoftapsrequired:2]; [Self.view ADDGESTURERECOGNizer:doubletapeve]; 1-Finger three-click event UITapGestureRecognizer *tripletap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector ( Tripletapaction:)]; [Tripletap setnumberoftouchesrequired:1]; [Tripletap Setnumberoftapsrequired:3]; [Self.view Addgesturerecognizer:tripletap]; [Doubletap Requiregesturerecognizertofail:tripletap];
4. Long Press
1. Create a long press gesture uilongpressgesturerecognizer *press = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (pressaction:)]; 2.1 Set the long press time press.minimumpressduration = 1; 2.2 Setting the number of fingers [Press setnumberoftouchesrequired:1]; 3. Add gesture to view //If add to Uilabel, Uiimageview etc. above, need to turn on user interaction to use _label.userinteractionenabled = YES; [_label addgesturerecognizer:press];// [Self.view addgesturerecognizer:press];
5. Kneading
-(void) viewdidload{ [Super Viewdidload]; 1. Create a pinch gesture (default kneading hand index is 2) uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] Initwithtarget:self Action: @selector (pinchaction:)]; 2. Add to view [_imageview addgesturerecognizer:pinch]; _imageview.userinteractionenabled = YES;} -(void) Pinchaction: (uipinchgesturerecognizer*) pinch{ _imageview.transform = Cgaffinetransformmakescale ( Pinch.scale, Pinch.scale); if (Pinch.scale = = uigesturerecognizerstateended) { _imageview.transform = Cgaffinetransformmakescale (1, 1);} }
6. Rotate
1. Create a rotation gesture uirotationgesturerecognizer *rotation = [[Uirotationgesturerecognizer alloc] Initwithtarget:self Action: @selector (rotationaction:)]; 2. Add gesture _imageview.userinteractionenabled = YES; [_imageview addgesturerecognizer:rotation];} -(void) Rotationaction: (uirotationgesturerecognizer*) rotation{ _imageview.transform = Cgaffinetransformmakerotation (rotation.rotation);}
7. Dragging
-(void) viewdidload{ [Super Viewdidload]; 1. Create a drag gesture uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] initwithtarget:self action: @selector ( Panaction:)]; 2. Add gestures to the view [_imageview Addgesturerecognizer:pan]; _imageview.userinteractionenabled = YES;} -(void) Panaction: (uipangesturerecognizer*) pan{ //1. Take the midpoint coordinates of the picture cgpoint centerpoint = _imageview.center; 2. Gets the offset position of the drag gesture relative to the current view cgpoint point = [Pan TranslationInView:self.view]; 3. Calculate the new coordinates of the picture _imageview.center = Cgpointmake (Centerpoint.x+point.x, centerpoint.y+point.y); 4. Every drag and drop, the translation is set to Cgpointzero, otherwise each drag will accumulate on the original basis if (pan.state = = uigesturerecognizerstatechanged) { [Pan Settranslation:cgpointzero InView:self.view]; }}
Ui:uigesturerecognizer use of gestures in iOS