手势识别 UIGestureRecognizer
- 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
- UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,,使用它的子类才能处理具体手势
- 手势分类
- UITapGestureRecognizer(点按)
- UIPinchGestureRecognizer(捏合)
- UIPanGestureRecognizer(拖动)
- UISwipeGestureRecognizer(轻扫)
- UIRotationGestureRecognizer(旋转)
- UILongPressGestureRecognizer(长按)
/**
手势识别,是单独添加到某一个视图上的
===================================
如果要同时支持多个手势识别,需要设置手势识别代理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
====================================
UIPanGestureRecognizer 拖动手势
UIPinchGestureRecognizer 缩放手势
UIRotationGestureRecognizer 旋转手势
都是通过transform设置视图的形变
一定要记住设置完transform之后,需要将对应的形变参数复位
=====================================
因为轻扫手势要求用户比较放松的扫动,因此最好不要将此手势添加到某一个视图上,会局限用户的操作
->一般要添加到self.view上
->如果要监听多个轻扫方向,需要添加多个轻扫手势
=====================================
长按手势一定要判断状态,否则方法会在手势开始和结束的时候分别调用,方法会被调用两次
*/
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation ViewController
- (UIImageView *)imageView
{
if(_imageView == nil){
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 400)];
_imageView.image = [UIImage imageNamed:@"头像1"];
//默认不支持点击的
_imageView.userInteractionEnabled = YES;
[self.view addSubview:_imageView];
}
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self imageView];
[self addLongPressGesture];
}
#pragma mark - 点按手势(单击,点击)
- (void)addTapGesture
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
//设置手势识别器对象的具体属性
// 连续敲击2次
tapGesture.numberOfTapsRequired = 2;
// 需要2根手指一起敲击
tapGesture.numberOfTouchesRequired = 2;
[self.imageView addGestureRecognizer:tapGesture];
}
- (void)tap
{
NSLog(@"摸我了");
}
#pragma mark - 旋转
- (void)addRotateGesture
{
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
//设置代理,想同时执行多个手势
rotate.delegate = self;
[self.imageView addGestureRecognizer:rotate];
}
- (void)rotate:(UIRotationGestureRecognizer *)recognizer
{
//recognizer.view 手势作用的视图
//recognizer.rotation 手势的旋转
//累加旋转之后,一定要将手势识别的旋转角度复位
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
// 将手势识别中的旋转角度复位
recognizer.rotation = 0.0; // 非常重要!
}
#pragma mark - 缩放-捏合
- (void)addPinchGesture
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
//设置代理,想同时执行多个手势的时候
pinch.delegate = self;
[self.imageView addGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
//将缩放比例复位
recognizer.scale = 1.0;
}
#pragma mark - 拖动手势
- (void)addPanGesture
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.imageView addGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.imageView];
recognizer.view.transform = CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y);
//给平移复位
[recognizer setTranslation:CGPointZero inView:self.imageView];
}
#pragma mark - 轻扫手势
- (void)addSwipeGesture
{
//如果要监听多个轻扫方向,需要提那家多个轻扫手势
//轻扫手势默认支持向右的扫动方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
//向左扫
UISwipeGestureRecognizer *swipeLeft =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//因为轻扫手势要求用户比较放松的扫动,因此最好不要将此手势添加到某一个视图上,会局限用户的操作
[self.view addGestureRecognizer:swipe];
[self.view addGestureRecognizer:swipeLeft];
}
- (void)swipe:(UISwipeGestureRecognizer *)recognizer
{
CGPoint from = self.imageView.center;
CGPoint to;
if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft){
to = CGPointMake(-from.x, from.y);
}else{
to = CGPointMake(3 * from.x, from.y);
}
[UIView animateWithDuration:0.25 animations:^{
self.imageView.center = to;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.25 animations:^{
self.imageView.center = from;
}];
}];
}
#pragma mark - 长按手势
- (void)addLongPressGesture
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer *)recognizer
{
//判断手势状态,长按手势一定要判断状态,否则方法会在手势开始和结束时分别调用!方法会被调用两次
if(recognizer.state == UIGestureRecognizerStateBegan){
[UIView animateWithDuration:0.25 animations:^{
self.imageView.alpha = 0.25;
} completion:^(BOOL finished) {
self.imageView.alpha = 1.0;
}];
}
}
#pragma mark - 手势识别代理方法
//同时识别两个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// 是否允许开始触发手势
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
// 是否允许接收手指的触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
// 获取当前的触摸点
CGPoint curP = [touch locationInView:self.imageView];
if (curP.x < self.imageView.bounds.size.width * 0.5) {
return NO;
}else{
return YES;
}
}
@end
//手势识别状态
typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
// 没有触摸事件发生,所有手势识别的默认状态
UIGestureRecognizerStatePossible,
// 一个手势已经开始但尚未改变或者完成时
UIGestureRecognizerStateBegan,
// 手势状态改变
UIGestureRecognizerStateChanged,
// 手势完成
UIGestureRecognizerStateEnded,
// 手势取消,恢复至Possible状态
UIGestureRecognizerStateCancelled,
// 手势失败,恢复至Possible状态
UIGestureRecognizerStateFailed,
// 识别到手势识别
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded
};