[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer

时间:2021-07-22 13:46:26
A.系统提供的手势识别器
[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
1.敲击手势 UITapGestureRecognizer
numberOfTapsRequired: 敲击次数
numberOfTouchesRequired: 同时敲击触碰数(手指数)
 - (void) testTap {
// 创建手势识别器
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)];
tapRec.numberOfTapsRequired = ; // 触发需要点击的次数
tapRec.numberOfTouchesRequired = ; // 触发需要同时点击的点数目 // 配置手势识别器到控件
[self.hvwView addGestureRecognizer: tapRec];
} /** tap手势的事件处理方法 */
- (void) tapRun:(UITapGestureRecognizer *) tapRec {
NSLog(@"tapRun");
}
2.长按手势 UILongPressGestureRecognizer
minimumPressDuration:长按生效时间
numberOfTouchesRequired:需要的同时点击数
numberOfTapsRequired:需要的点击次数
allowableMovement:长按有效移动范围(从点击开始,长按移动的允许范围)
ps:移动的时候会不断调用目标方法
 - (void) testLongPress {
UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)]; longRec.minimumPressDuration = ; // 长按生效时间
longRec.allowableMovement = ; // 长按允许移动范围,单位:px [self.hvwView addGestureRecognizer:longRec];
}
3.轻扫手势 UISwipeGestureRecognizer
direction:扫的方向 上下左右
 typedef enum {
UISwipeGestureRecognizerDirectionRight = << ,
UISwipeGestureRecognizerDirectionLeft = << ,
UISwipeGestureRecognizerDirectionUp = << ,
UISwipeGestureRecognizerDirectionDown = <<
} UISwipeGestureRecognizerDirection;
 - (void) testSwipe {
UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向 [self.hvwView addGestureRecognizer:swipeRec];
}
4.捏合手势 UIPinchGestureRecognizer
scale: 捏合的距离
 - (void) testPinch {
UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)]; [self.hvwView addGestureRecognizer:rec];
} - (void) pinchRun:(UIPinchGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale); // 一定要复位!!!不然按住捏合会不断叠加倍数
rec.scale = ;
}
[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer  [iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
5.旋转手势 UIRotationGestureRecognizer
rotation: 旋转了的角度
 - (void) testRotation {
UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)]; [self.hvwView addGestureRecognizer:rotationRec];
} - (void) rotationRun:(UIRotationGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformRotate(rec.view.transform, rec.rotation); // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation
rec.rotation = ;
}
[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
6.拖曳手势 UIPanGestureRecognizer
相对始点拖曳距离:[pan translationInView:pan.view]
 - (void) testPan {
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)]; [self.hvwView addGestureRecognizer:rec];
} - (void) panRun:(UIPanGestureRecognizer *) rec {
// 取得拖曳距离
CGPoint movedDistance = [rec translationInView:rec.view];
CGPoint viewCenter = rec.view.center;
viewCenter.x += movedDistance.x;
viewCenter.y += movedDistance.y;
rec.view.center = viewCenter; // 复位拖曳距离
[rec setTranslation:CGPointZero inView:rec.view];
}
[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
B.一般情况只能使用一个识别器,使用代理设置才能同时使用多个识别器
[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
 
1.遵守协议:
 @interface ViewController () <UIGestureRecognizerDelegate>
2.给需要同时进行识别的手势识别器设置代理
 rec.delegate = self;
3.实现手势识别器同时使用方法
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

     // 任何两种手势识别器都能同时使用
return YES;
}
 //
// ViewController.m
// GestureRecognizerTest
//
// Created by hellovoidworld on 15/1/13.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIView *hvwView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // [self testTap];
// [self testLongPress];
// [self testSwipe];
[self testRotation];
[self testPinch];
[self testPan];
} - (void) testTap {
// 创建手势识别器
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)];
tapRec.numberOfTapsRequired = ; // 触发需要点击的次数
tapRec.numberOfTouchesRequired = ; // 触发需要同时点击的点数目 // 配置手势识别器到控件
[self.hvwView addGestureRecognizer: tapRec];
} /** tap手势的事件处理方法 */
- (void) tapRun:(UITapGestureRecognizer *) tapRec {
NSLog(@"tapRun");
} - (void) testLongPress {
UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)]; longRec.minimumPressDuration = ; // 长按生效时间
longRec.allowableMovement = ; // 长按允许移动范围,单位:px [self.hvwView addGestureRecognizer:longRec];
} - (void) longPressRun:(UILongPressGestureRecognizer *) rec {
NSLog(@"longPress");
} - (void) testSwipe {
UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向 [self.hvwView addGestureRecognizer:swipeRec];
} - (void) swipeRun:(UISwipeGestureRecognizer *) rec {
NSLog(@"swipe");
} - (void) testRotation {
UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)];
rotationRec.delegate = self; [self.hvwView addGestureRecognizer:rotationRec];
} - (void) rotationRun:(UIRotationGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformRotate(self.hvwView.transform, rec.rotation); // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation
rec.rotation = ;
} - (void) testPinch {
UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)];
rec.delegate = self; [self.hvwView addGestureRecognizer:rec];
} - (void) pinchRun:(UIPinchGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale); // 一定要复位!!!不然按住捏合会不断叠加倍数
rec.scale = ;
} - (void) testPan {
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)];
rec.delegate = self; [self.hvwView addGestureRecognizer:rec];
} - (void) panRun:(UIPanGestureRecognizer *) rec {
// 取得拖曳距离
CGPoint movedDistance = [rec translationInView:rec.view];
CGPoint viewCenter = rec.view.center;
viewCenter.x += movedDistance.x;
viewCenter.y += movedDistance.y;
rec.view.center = viewCenter; // 复位拖曳距离
[rec setTranslation:CGPointZero inView:rec.view];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { // 任何两种手势识别器都能同时使用
return YES;
} @end
#mark:
因为使用的是transform形变进行缩放、旋转,所以再进行拖曳的时候是不能正常进行的。