iOS Development gestures--uigesturerecognizer co-exist in the development of the IPhone or IPad, in addition to using the Touchesbegan/touchesmoved/touchesended method to control the user's finger touch, can also be used Uigesturerecognizer of the derivative classes to be judged. The advantage of using Uigesturerecognizer is that there are ready-made gestures, and developers don't have to calculate their finger movements. There are several derivative types of Uigesturerecognizer: UITapGestureRecognizer uipinchgesturerecognizer Uirotationgesturerecognizer Uiswipegesturerecognizer Uipangesturerecognizer Uilongpressgesturerecognizer It is not difficult to know the gestures of the corresponding representatives of these categories, 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 the code//define a recognizer and add it to the UIView element that needs to detect the gesture-(void) viewdidload {uiswipegesturerecognizer* recognizer;//HANDLESWIPEF Rom is the method that detects the gesture, the 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 = Uiswipegesturerecogniz Erdirectionup [Self.view Addgesturerecognizer:recognizer]; [Recognizer release]; }-(void) HandleswipefroM: (uiswipegesturerecognizer*) recognizer {//trigger a hand event, do something here//below is the method of removing gestures [self.view removegesturerecognizer: Recognizer]; The problem with copying the code. 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 as an example, the code is as follows: Copy code-(void) Viewdidload {//click 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]; The key is in this row, if double-clicking to determine the failure of the detector will trigger click [Singlerecognizer Requiregesturerecognizertofail:doublerecognizer]; [Singlerecognizer release]; [Doublerecognizer release];
Gesture-uigesturerecognizer Coexistence of iOS development