Uigesturerecognizer
In order to complete gesture recognition, the gesture recognizer must be used--uigesturerecognizer
With Uigesturerecognizer, you can easily identify some common gestures that users make on a view
Uigesturerecognizer is an abstract class that defines the basic behavior of all gestures and uses its subclasses to handle specific gestures
UITapGestureRecognizer //(敲击)UIPinchGestureRecognizer //(捏合,用于缩放)UIPanGestureRecognizer //(拖拽)UISwipeGestureRecognizer //(轻扫)UIRotationGestureRecognizer //(旋转)//(长按)
Percussion UITapGestureRecognizer
Each gesture recognizer uses a similar set of steps, such as UITapGestureRecognizer:
创建手势识别器对象UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];设置手势识别器对象的具体属性// 连续敲击2次tap.numberOfTapsRequired2;// 需要2根手指一起敲击tap.numberOfTouchesRequired2;添加手势识别器到对应的view上[self.iconView addGestureRecognizer:tap];监听手势的触发[tap addTarget:self action:@selector(tapIconView:)];
Percussion instance
@interface grviewcontroller () <uigesturerecognizerdelegate>@property(Weak,nonatomic)Iboutlet Uiimageview*iconview;@end @implementation grviewcontroller - (void) viewdidload{[SuperViewdidload]; [ SelfTESTTAP2];} - (void) testtap2{Uigesturerecognizer *tap = [[UITapGestureRecognizer alloc] Initwithtarget: SelfAction@selector(Tapview)]; Tap. Delegate= Self; [ Self. IconViewADDGESTURERECOGNIZER:TAP];}#pragma mark-Proxy method/** * When clicking on the view, this method is called first */- (BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldreceivetouch: (Uitouch *) touch{Cgpointpos = [Touch Locationinview:touch. View];if(POS. x<= Self. IconView. Frame. Size. Width*0.5) {return YES; }return NO;} - (void) testtap{//1. Create a gesture Recognizer ObjectUITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];//2 consecutive strokes to identify successTap. numberoftapsrequired=2; Tap. numberoftouchesrequired=2;//2. Add a gesture Recognizer object to the corresponding view[ Self. IconViewADDGESTURERECOGNIZER:TAP];//3. Add a listening method (the corresponding gesture is recognized, the listener method is called)[Tap AddTarget: SelfAction@selector(Tapview)];} - (void) tapview{NSLog(@"-----I tapped the screen!! ");}@end
Long press and swipe instances
@interface grviewcontroller ()@property(Weak,nonatomic)Iboutlet UIView*redview;@end @implementation grviewcontroller - (void) viewdidload{[SuperViewdidload]; Uiswipegesturerecognizer *swipe = [[Uiswipegesturerecognizer alloc] Initwithtarget: SelfAction@selector(Swipeview)]; Swipe. Direction= Uiswipegesturerecognizerdirectionup; [ Self. RedviewAddgesturerecognizer:swipe];} - (void) swipeview{NSLog(@"Swipeview");} - (void) testlongpress{Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] init]; [Longpress AddTarget: SelfAction@selector(Longpressview)];//Long press for at least 2 secondsLongpress. Minimumpressduration=2;//Before triggering gestures, long press in 50px rangeLongpress. Allowablemovement= -; [ Self. RedviewAddgesturerecognizer:longpress];} - (void) longpressview{NSLog(@"Long pressed the red view");}@end
Scaling and rotating instances
@interface grviewcontroller () <uigesturerecognizerdelegate>@property(Weak,nonatomic)Iboutlet Uiimageview*iconview;@end @implementation grviewcontroller - (void) viewdidload{[SuperViewdidload]; [ SelfTestpinchandrotate];}Proxy method for #pragma mark-gesture recognizer/** * Allows multiple gesture recognizers to be active simultaneously * simultaneously: simultaneous */- (BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: ( Uigesturerecognizer *) othergesturerecognizer{return YES;}#pragma mark-zoom + rotate- (void) testpinchandrotate{[ SelfTestpinch]; [ SelfTestrotate];}#pragma mark-zoom gesture (pinch gesture)- (void) testpinch{Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc] Initwithtarget: SelfAction@selector(Pinchview:)]; Pinch. Delegate= Self; [ Self. IconViewAddgesturerecognizer:pinch];} - (void) Pinchview: (Uipinchgesturerecognizer *) pinch{pinch. View. Transform= Cgaffinetransformscale (pinch. View. Transform, pinch. scale, pinch. scale); Pinch. scale=1;//This is really important!!!!!}#pragma mark-rotate gesture- (void) testrotate{Uirotationgesturerecognizer *recognizer = [[Uirotationgesturerecognizer alloc] InitWithTarget: SelfAction@selector(Rotateview:)]; Recognizer. Delegate= Self; [ Self. IconViewAddgesturerecognizer:recognizer];} - (void) Rotateview: (Uirotationgesturerecognizer *) recognizer{recognizer. View. Transform= Cgaffinetransformrotate (recognizer. View. Transform, recognizer. Rotation); Recognizer. Rotation=0;//This is important!!!!!}@end
Drag and Drop instances
@interface grviewcontroller ()@property(Weak,nonatomic)Iboutlet UIView*purpleview;@end @implementation grviewcontroller - (void) viewdidload{[SuperViewdidload]; Uipangesturerecognizer *pan = [[Uipangesturerecognizer alloc] Initwithtarget: SelfAction@selector(Panview:)]; [ Self. PurpleviewAddgesturerecognizer:pan];} - (void) Panview: (Uipangesturerecognizer *) pan{Switch(Pan. State) { CaseUigesturerecognizerstatebegan://Start trigger gesture Break; CaseUigesturerecognizerstateended://Gesture End Break;default: Break; }//1. Distance on view Cgpointtranslation = [Pan Translationinview:pan. View];CgpointCenter = Pan. View. Center; Center. x+ = Translation. x; Center. Y+ = Translation. Y; Pan. View. Center= Center;//2. Clear the Moving distance[Pan Settranslation:cgpointzero Inview:pan. View];}@end
Status of gesture recognition
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureRecognizerStatePossible, // 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateBegan, // 手势状态改变 UIGestureRecognizerStateChanged, // 手势完成 UIGestureRecognizerStateEnded, // 手势取消,恢复至Possible状态 UIGestureRecognizerStateCancelled, // 手势失败,恢复至Possible状态 UIGestureRecognizerStateFailed, // 识别到手势识别 UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded};
iOS Development-gesture recognition (Uigesturerecognizer)