Gestures in IOS
1. Click
UITapGestureRecognizer * tap = [[delealloc] initWithTarget: self action: @ selector (click)]; // set the number of times to be clicked [tap setNumberOfTapsRequired: 1]; // set the number of fingers for the current event to be triggered [tap setNumberOfTouchesRequired: 2]; // set the current proxy tap. delegate = self; [_ view addGestureRecognizer: tap]; // trigger method-(void) click {NSLog (@ "the current view has been clicked! ");}
2. Long press
UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @ selector (longPress)]; // sets longPress with the minimum length for the current duration. minimumPressDuration = 2; // set the allowable range of movement [longPress setAllowableMovement: 2]; [_ view addGestureRecognizer: longPress]; // trigger method-(void) longPress {NSLog (@ "triggered by long press event! ");}
3. Sweep
UISwipeGestureRecognizer * swip = [[UISwipeGestureRecognizer alloc] initWithTarget: self action: @ selector (swipMethod)]; // swip to the left. direction = uiswipegesturerecognizerdireleft; // swip to the right. direction = UISwipeGestureRecognizerDirectionRight; // swip to the top. direction = uiswipegesturerecognizerdireup up; // swip to the following direction. direction = UISwipeGestureRecognizerDirectionDown; [_ view addGestureRecognizer: swip] ; // Trigger method-(void) swipMethod {NSLog (@ "triggered by a light scan event! ");}
If two or more directions are involved, it is best to add multiple UISwipeGestureRecognizer objects and set different directions. Do not use the symbol | to connect them in the following way:
swip.direction=UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight
4. Drag
Step 1: Add a view
_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];[_view setBackgroundColor:[UIColor redColor]];[self.view addSubview:_view];
Step 2: Add a gesture
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(paned:)];[_view addGestureRecognizer:pan];
Step 3: Implementation Method
-(Void) paned :( UIPanGestureRecognizer *) pan {// obtain the moving size CGPoint point = [pan translationInView: pan. view]; // modify the central point coordinate CGPoint points = _ view of the view. center; points. x + = point. x; points. y + = point. y; _ view. center = points; // clear the coordinates every time to eliminate the superposition [pan setTranslation: CGPointZero inView: pan. view];}
5. Rotate
Step 1: Add a view
_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];[_view setBackgroundColor:[UIColor redColor]];[self.view addSubview:_view];
Step 2: Add a gesture
UIRotationGestureRecognizer * roate=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)]; [_view addGestureRecognizer:roate];roate.delegate=self;
Step 3: Implementation Method
-(Void) rotate :( UIRotationGestureRecognizer *) rote {// obtain the current rotation degree CGFloat rotation = rote. rotation; // rotate _ view by means of the affine transform. transform = CGAffineTransformRotate (_ view. transform, rotation); // prevents rotation overlay from needing to be cleared. rotation = 0 ;}
6. kneading
Step 1: Add a view
_view=[[UIView alloc] initWithFrame:CGRectMake(50, 250, 300, 200)];[_view setBackgroundColor:[UIColor redColor]];[self.view addSubview:_view];
Step 2: Add a gesture
UIPinchGestureRecognizer * pich=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(piches:)];[_view addGestureRecognizer:pich];pich.delegate=self;
Step 3: Implementation Method
-(Void) piches :( UIPinchGestureRecognizer *) pich {// obtain the proportion CGFloat scale = pich. scale; // scale _ view by using the affine transform. transform = CGAffineTransformScale (_ view. transform, scale, scale); // prevents proportional superposition from being set to 1 pich. scale = 1 ;}
[Supplement] rewrite the proxy method if you need to respond to multiple gestures at the same time
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES;}