IOS gesture Uigesturerecognizer

Source: Internet
Author: User
Tags uikit



In the development of the IPhone or IPad, in addition to using the Touchesbegan/touchesmoved/touchesended method to control the user's finger touch, you can also use the Uigesturerecognizer derivative class to judge. The advantage of using Uigesturerecognizer is that there are ready-made gestures, and developers don't have to calculate their finger movements. Uigesturerecognizer have the following types of derivatives:


UITapGestureRecognizer
Uipinchgesturerecognizer
Uirotationgesturerecognizer
Uiswipegesturerecognizer
Uipangesturerecognizer
Uilongpressgesturerecognizer


It is not difficult to know the gestures of these classes of representatives, such as TAP (Point), Pinch (two fingers inward or outward), Rotation (rotate), Swipe (sliding, fast moving), Pan (drag, slow motion), and Longpress (Long Press). These gestures are also very simple to use, as long as they are defined and added to the corresponding view before use.


Copy Code
Define a recognizer and add it to the UIView element that needs to detect the gesture
-(void) Viewdidload {
uiswipegesturerecognizer* recognizer;
Handleswipefrom is a way to detect gestures, to call.
recognizer = [[Uiswipegesturerecognizer alloc] initwithtarget:selfaction: @selector (Handleswipefrom)];
Different recognizer have different entity variables
For example, Swipegesture can specify the direction
And tapgesture can specify the number of times
Recognizer.direction = Uiswipegesturerecognizerdirectionup
[Self.view Addgesturerecognizer:recognizer];
[Recognizer release];
}


-(void) Handleswipefrom: (uiswipegesturerecognizer*) Recognizer {
After triggering a hand event, do something here


The bottom is the way to remove gestures.
[Self.view Removegesturerecognizer:recognizer];
}
Copy Code
The problem comes. Some gestures are actually interrelated, such as tap and longpress, swipe and Pan, or tap once and tap twice. When a UIView adds two associated gestures at the same time, is it a tap or a longpress on my finger? If the preset practice, as long as "meet the conditions" will jump out and call the corresponding method, for example, if you register both pan and Swipe, as long as the finger moves will trigger the Pan and then jump out, and thus will never happen Swipe, single point and two points of the same situation, It will always only trigger a single point, not a double point.


So is there a solution to this problem? The answer is yes, Uigesturerecognizer has a method called Requiregesturerecognizertofail, he can designate a certain recognizer, even if he is already satisfied with the condition, and will not be triggered immediately, would wait until the specified Recognizer is not triggered until it determines the failure. To support both single-point and double-point gestures, for example, the code is as follows:


Copy Code
-(void) Viewdidload {
Click the recognizer
uitapgesturerecognizer* Singlerecognizer;
Singlerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Handlesingletapfrom)];
singletaprecognizer.numberoftapsrequired = 1; Click
[Self.view Addgesturerecognizer:singlerecognizer];


Double-click the recognizer
Uitapgesturerecognizer* Double;
Doublerecognizer = [[UITapGestureRecognizer alloc] initwithtarget:selfaction: @selector (Handledoubletapfrom)];
doubletaprecognizer.numberoftapsrequired = 2; Double click
[Self.view Addgesturerecognizer:doublerecognizer];


Key in this row, if you double-click to determine if the reconnaissance failure will trigger the click
[Singlerecognizer Requiregesturerecognizertofail:doublerecognizer];
[Singlerecognizer release];
[Doublerecognizer release];
}
Demo
First create a new project based on the Sigle view application, named Gesturetest; my project structure is as follows:






Drag a ImageView to the Viewcontroller.xib file and make the entire screen, and change the properties to:






ViewController.h file:








1. #import <UIKit/UIKit.h>


2.


3. @interface viewcontroller:uiviewcontroller{


4. Iboutlet Uiimageview *imageview;


5.}


6. @property (nonatomic,retain) iboutlet Uiimageview *imageview;


7. @end


And make the ImageView in the Xib file connected with it;





Then the implementation section of the VIEWCONTROLLER.M file:








1. @synthesize ImageView;


2.


3. CGFloat lastscalefactor=1;//zoom in, zoom out


4. CGFloat netrotation;//Rotation


5. Cgpoint nettranslation;//Balance


6. Nsarray *images;//Image Array


7. int imageindex=0;//array subscript


8.


9.-(void) viewdidload


10. {


11.//1, create gesture instance, and connect method Handletapgesture, click Gesture


UITapGestureRecognizer *tapgesture=[[uitapgesturerecognizer alloc]initwithtarget:self Action: @selector ( Handletapgesture:)];


13.//Set gesture hit number, double click: Point 2


tapgesture.numberoftapsrequired=2;


ImageView//Add gesture recognition


[ImageView Addgesturerecognizer:tapgesture];


17.//Release memory


[Tapgesture release];


19.


20.//2, gesture for pinch posture: Hold the option button with the mouse to do this action on the virtual device


Uipinchgesturerecognizer *pinchgesture=[[uipinchgesturerecognizer alloc]initwithtarget:self Action: @selector ( Handlepinchgesture:)];


[ImageView addgesturerecognizer:pinchgesture];//imageview Add gesture recognition


[Pinchgesture release];


24.


25.//3, rotate gesture: Hold the option button with the mouse to do this action on the virtual device


Uirotationgesturerecognizer *rotategesture=[[uirotationgesturerecognizer alloc]initwithtarget:self action:@ Selector (handlerotategesture:)];


[ImageView Addgesturerecognizer:rotategesture];


[Rotategesture release];


29.


30.//4, drag gestures


Uipangesturerecognizer *pangesture=[[uipangesturerecognizer alloc]initwithtarget:self Action: @selector ( Handlepangesture:)];


//[ImageView addgesturerecognizer:pangesture];


[Pangesture release];


34.


35.//5, swipe gesture


Images=[[nsarray alloc]initwithobjects:@ "cell.jpg", @ "heihua.jpg", @ "xuanyi.jpg", nil];


37.//Right Stroke


Uiswipegesturerecognizer *swipegesture=[[uiswipegesturerecognizer alloc]initwithtarget:self Action: @selector ( Handleswipegesture:)];


[ImageView Addgesturerecognizer:swipegesture];


[Swipegesture release];


41.//Left Stroke


Uiswipegesturerecognizer *swipeleftgesture=[[uiswipegesturerecognizer alloc]initwithtarget:self action:@ Selector (handleswipegesture:)];


swipegesture.direction=uiswipegesturerecognizerdirectionleft;//not set the night is right


[ImageView Addgesturerecognizer:swipeleftgesture];


[Swipeleftgesture release];


46.


47.//6, Long press gestures


Uilongpressgesturerecognizer *longpressgesutre=[[uilongpressgesturerecognizer alloc]initWithTarget:self Action : @selector (handlelongpressgesture:)];


49.//long-press time is 1 seconds


Longpressgesutre.minimumpressduration=1;


51.//Allow 15 seconds of movement


longpressgesutre.allowablemovement=15;


53.//Required Touch 1 times


longpressgesutre.numberoftouchesrequired=1;


[ImageView Addgesturerecognizer:longpressgesutre];


[Longpressgesutre release];


57.


[Super Viewdidload];


Additional setup after loading the view, typically from a nib.


60.}


61.//This method is called when you double-click the screen to enlarge and shrink the picture


-(Ibaction) Handletapgesture: (uigesturerecognizer*) sender{


63.//Determine if the content mode of the ImageView is Uiviewcontentmodescaleaspectfit, the mode is the original scale, according to the picture original time scale display size


if (Sender.view.contentmode==uiviewcontentmodescaleaspectfit) {


65.//Change the ImageView mode to Uiviewcontentmodecenter and display a part of the center according to the original size of the picture in ImageView


Sender.view.contentmode=uiviewcontentmodecenter;


}else{.


Sender.view.contentmode=uiviewcontentmodescaleaspectfit;


69.}


70.}


71.//pinch gesture, make the picture enlarge and shrink, pinch the action is a continuous action


-(Ibaction) Handlepinchgesture: (uigesturerecognizer*) sender{


73.//Get sender pinch gesture size


CGFloat factor=[(uipinchgesturerecognizer*) sender scale];


if (factor>1) {


76.//Image magnification


Sender.view.transform=cgaffinetransformmakescale (lastscalefactor+ (factor-1), (lastscalefactor+ (factor-1)));


78.


}else{.


80.//Zoom Out


Bayi. Sender.view.transform=cgaffinetransformmakescale (Lastscalefactor*factor, lastscalefactor*factor);


82.


83.}


84.//Whether the status is over, and if the data is saved


. if (sender.state==uigesturerecognizerstateended) {


if (factor>1) {


lastscalefactor+= (FACTOR-1);


}else{.


Lastscalefactor*=factor;


90.}


91.}


92.}


93.//Rotation gesture


94.-(Ibaction) Handlerotategesture: (uigesturerecognizer*) sender{


95.//float type, get sender's rotation degree


CGFloat rotation=[(uirotationgesturerecognizer*) sender rotation];


97.//Rotation Angle cgaffinetransformmakerotation


98. Cgaffinetransform transform=cgaffinetransformmakerotation (rotation+netrotation);


99.//Change the image angle


Sender.view.transform=transform;


101.//Status end, save data


102. if (sender.state==uigesturerecognizerstateended) {


103. Netrotation+=rotation;


104.}


105.


106.}


107.//Drag gestures


108.-(Ibaction) Handlepangesture: (uigesturerecognizer*) sender{


109.//Get the XY coordinates in the drag process


Cgpoint translation=[(uipangesturerecognizer*) sender Translationinview:imageview];


111.//Panning Picture Cgaffinetransformmaketranslation


Sender.view.transform=cgaffinetransformmaketranslation (nettranslation.x+translation.x, netTranslation.y+ TRANSLATION.Y);


113.//Status end, save data


if (sender.state==uigesturerecognizerstateended) {


Nettranslation.x+=translation.x.


NETTRANSLATION.Y+=TRANSLATION.Y;


117.}


118.


119.}


120.//Swipe gesture


121.-(Ibaction) Handleswipegesture: (uigesturerecognizer*) sender{


122.//Direction of stroke


123. Uiswipegesturerecognizerdirection direction=[(uiswipegesturerecognizer*) sender direction];


124.//judgment is up or down


. switch (direction) {


126. Case Uiswipegesturerecognizerdirectionup:


127. NSLog (@ "up");


. break;


129. Case Uiswipegesturerecognizerdirectiondown:


NSLog (@ "down");


131. break;


Case Uiswipegesturerecognizerdirectionleft:


133. NSLog (@ "left");


134. imageindex++;//Subscript + +


135. Break;


136. Case Uiswipegesturerecognizerdirectionright:


137. NSLog (@ "right");


138. imageindex--;//Subscript--


139. Break;


. Default:


141. Break;


142.}


143.//Get subscript that does not cross the <0


144. imageindex= (imageindex<0)? ([Images count]-1): imageindex%[images Count];


145.//imageview Display Pictures


146. Imageview.image=[uiimage Imagenamed:[images Objectatindex:imageindex];


147.


148.}


149.//Long Press gestures


-(Ibaction) Handlelongpressgesture: (uigesturerecognizer*) sender{


151.//Create a warning


Uiactionsheet *actionsheet=[[uiactionsheet alloc]initwithtitle:@ "Image options" Delegate:self cancelButtonTitle : Nil destructivebuttontitle:nil otherbuttontitles:@ "Save Image" @ "Copy", nil];


153.//Current View display warning


154. [Actionsheet ShowInView:self.view];


155. [Actionsheet release];


156.}


157.-(void) dealloc{


158. [Images release];


159. [ImageView release];


[Super Dealloc];


161.}

IOS gesture Uigesturerecognizer

Related Article

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.