when developing iOS apps we often use multiple gestures to handle things, such as adding click events to ScrollView, ScrollView not responding to view touch events, but sometimes using multi-gesture events, Then we can give this scrollview an additional finger event. Adding clicks, magnification, and rotation to ImageView can be done with multiple gestures. The following is a description of the use of various gestures, and precautions. - (void) viewdidload {[Super viewdidload]; //in order to respond to multi-gesture events, ImageView's Userinteractionenabled property is set to Yes.Self.imageview.userinteractionenabled=YES; //1. Finger Click event//single-Finger clickUITapGestureRecognizer *singlefingerone =[[UITapGestureRecognizer alloc] Initwithtarget:self Action: @selector (fingerincident:)];//Number of fingerssinglefingerone.numberoftouchesrequired =1;//Click Countsinglefingerone.numberoftapsrequired =1; //Set proxy MethodsSinglefingerone.Delegate=Self ; //Add Event Responder,[Self.imageview Addgesturerecognizer:singlefingerone]; //single-finger double-tapUITapGestureRecognizer *singlefingertwo =[[UITapGestureRecognizer alloc] Initwithtarget:self Action: @selector (fingerincident:)]; Singlefingertwo.numberoftouchesrequired=1; Singlefingertwo.numberoftapsrequired=2; Singlefingertwo.Delegate=Self ; [Self.imageview Addgesturerecognizer:singlefingertwo]; //two-Finger clickUITapGestureRecognizer *doublefingerone =[[UITapGestureRecognizer alloc] Initwithtarget:self Action: @selector (fingerincident:)]; Doublefingerone.numberoftouchesrequired=2; Doublefingerone.numberoftapsrequired=1; Doublefingerone.Delegate=Self ; [Self.imageview Addgesturerecognizer:doublefingerone]; //Double-fingered double-tapUITapGestureRecognizer *doublefingertwo =[[UITapGestureRecognizer alloc] Initwithtarget:self Action: @selector (fingerincident:)]; Doublefingertwo.numberoftouchesrequired=2; Doublefingertwo.numberoftapsrequired=2; Doublefingertwo.Delegate=Self ; [Self.imageview Addgesturerecognizer:doublefingertwo]; //If you do not add the following, when a single-finger double-click, the first call to single-hit processing, and then call a single-finger double-hit processing[Singlefingerone Requiregesturerecognizertofail:singlefingertwo]; //The same is true of two fingers .[Doublefingerone Requiregesturerecognizertofail:doublefingertwo]; //2, gesture for pinch posture: Hold the option button with the mouse to do this action on the virtual deviceUipinchgesturerecognizer *pinchgesture=[[Uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector (handlepinchgesture:)]; [Self.imageview addgesturerecognizer:pinchgesture];//ImageView adding gesture recognition[Pinchgesture release]; //3. Rotate gesture: Hold the option button with the mouse to do this action on the virtual deviceUirotationgesturerecognizer *rotategesture=[[Uirotationgesturerecognizer alloc]initwithtarget:self Action: @selector (handlerotategesture:)]; [Self.imageview Addgesturerecognizer:rotategesture]; [Rotategesture release]; //4. Drag gesturesUipangesturerecognizer *pangesture=[[Uipangesturerecognizer alloc]initwithtarget:self Action: @selector (handlepangesture:)]; [Self.imageview Addgesturerecognizer:pangesture]; [Pangesture release]; //when you implement the above drag gesture (Uipangesturerecognizer), you will not be able to respond to the following left and right stroke (Uiswipegesturerecognizer) events, and the event will be truncated by the drag gesture bar, so the left and right strokes and drag gestures can only be selected .//Right StrokeUiswipegesturerecognizer *swipegesture=[[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (handleswipegesture:)]; [Self.imageview Addgesturerecognizer:swipegesture]; [Swipegesture release]; //Left StrokeUiswipegesturerecognizer *swipeleftgesture=[[Uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector (handleswipegesture:)]; Swipegesture.direction=uiswipegesturerecognizerdirectionleft;//do not set the night is right[Self.imageview addgesturerecognizer:swipeleftgesture]; [Swipeleftgesture release]; Uilongpressgesturerecognizer*longpressgesutre=[[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (handlelongpressgesture:)]; //long-press time is 1 secondslongpressgesutre.minimumpressduration=0.5; //allows movement in 15 secondsLongpressgesutre.allowablemovement=3; //Required Touch 1 timesLongpressgesutre.numberoftouchesrequired=1; [Self.imageview Addgesturerecognizer:longpressgesutre]; [Longpressgesutre release];}//Finger Click event- (void) Fingerincident: (UITapGestureRecognizer *) sender{if(sender.numberoftouchesrequired==1) { //single-Finger click events if(sender.numberoftapsrequired = =1) { //single-Finger clickNSLog (@"single-Finger click"); [Sender.view.layer removeallanimations]; } Else if(sender.numberoftapsrequired = =2 ){ //single-finger double-tapNSLog (@"single-finger double-tap"); } } Else if(sender.numberoftouchesrequired==2) { //Two-Finger Click event if(sender.numberoftapsrequired = =1) { //two-Finger clickNSLog (@"two-Finger click"); } Else if(sender.numberoftapsrequired = =2 ){ //Double-fingered double-tapNSLog (@"Double-fingered double-tap"); } }}//finger swipe left and right events-(void) Handleswipegesture: (Uiswipegesturerecognizer *) sender{if(sender.direction==uiswipegesturerecognizerdirectionleft) {NSLog (@"Left Slide"); } Else{NSLog (@"Right Slide"); }}//Finger Pinch events-(void) Handlepinchgesture: (Uipinchgesturerecognizer *) sender{NSLog (@"Pinch"); if([sender state] = =uigesturerecognizerstateended) {Lastscale=1.0; return; } cgfloat Scale=1.0-(Lastscale-[(uipinchgesturerecognizer*) (sender scale]); Cgaffinetransform Newtransform=Cgaffinetransformscale (sender.view.transform, scale, scale); [Sender.view Settransform:newtransform]; Lastscale=[sender scale];}//Finger Spin Events-(void) Handlerotategesture: (Uirotationgesturerecognizer *) sender{NSLog (@"Rotate"); NSLog (@"sender.scale=%f", sender.rotation); NSLog (@"sender.velocity=%f", sender.velocity); Sender.view.transform=cgaffinetransformrotate (Sender.view.transform, sender.rotation); Sender.rotation=0;}//Finger Drag Events-(void) Handlepangesture: (Uipangesturerecognizer *) sender{NSLog (@"Drag"); NSLog (@"sender=%@", sender); Cgpoint translation=[(uipangesturerecognizer*) sender TranslationInView:self.view]; //Start if(sender.state==Uigesturerecognizerstatebegan) {x=sender.view.center.x; Y=Sender.view.center.y; } //in Progress if(sender.state==uigesturerecognizerstatechanged) {Sender.view.center=cgpointmake (x+translation.x, y+TRANSLATION.Y); } //End if(sender.state==uigesturerecognizerstateended) {Sender.view.center=cgpointmake (x+translation.x, y+TRANSLATION.Y); }}//Finger Long Press event-(void) Handlelongpressgesture: (Uilongpressgesturerecognizer *) sender{NSLog (@"Long Press"); if(sender.state==Uigesturerecognizerstatebegan) {cabasicanimation* Rotationanimation_ = [cabasicanimation animationwithkeypath:@"transform.rotation.z"]; //Angle to radiansRotationanimation_.fromvalue = [NSNumber numberwithfloat: (-0.02)]; Rotationanimation_.tovalue= [NSNumber numberwithfloat: (0.02)]; //Animation TimeRotationanimation_.duration =0.1f; //Number of animationsRotationanimation_.repeatcount=Flt_max; //Fallback AnimationsRotationanimation_.autoreverses =YES; //the speed at which the animation begins to end, set to accelerateRotationanimation_.timingfunction =[Camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; [[Sender.view layer] addanimation:rotationanimation_ forkey:@"revitupanimation"]; }}
iOS multi-gesture events