1.手势识别概述
- 如果想监听一个view上面的触摸事件,之前的做法是
- 自定义一个view
- 实现view的touches方法,在方法内部实现具体处理代码
- 但是通过touches方法监听view触摸事件,有很明显的几个缺点
- 必须得自定义view
- 由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
- 不容易区分用户的具体手势行为
- iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度
2.UIGestureRecognizer(手势识别器)
- 为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer
- 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
- UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
- UITapGestureRecognizer(敲击)
- UIPinchGestureRecognizer(捏合,用于缩放)
- UIPanGestureRecognizer(拖拽)
- UISwipeGestureRecognizer(轻扫)
- UIRotationGestureRecognizer(旋转)
- UILongPressGestureRecognizer(长按)
3.手势识别器的基本使用
- UITapGestureRecognizer(点击手势)
#pragma mark - 添加点击手势
-(void)setUpTap{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.delegate = self;
[self.redView addGestureRecognizer:tap];
}
#pragma mark -UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint curP = [touch locationInView:self.redView];
if (curP.x > self.redView.bounds.size.width * 0.5) {
return NO;
}else{
return YES;
}
}
-(void)tap:(UITapGestureRecognizer *)tap{
NSLog(@"敲击手势");
}
- UIPinchGestureRecognizer(捏合手势)
#pragma mark - 添加捏合手势
-(void)setUpPin{
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];
[self.redView addGestureRecognizer:pin];
}
-(void)pin:(UIPinchGestureRecognizer *)pin{
self.redView.transform = CGAffineTransformScale(self.redView.transform,pin.scale, pin.scale);
pin.scale = 1.0;
}
- UISwipeGestureRecognizer(轻扫手势)
#pragma mark - 添加左右轻扫手势
-(void)setUpSwipe{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.redView addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
swipe1.direction = UISwipeGestureRecognizerDirectionRight;
[self.redView addGestureRecognizer:swipe1];
}
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){
NSLog(@"向左扫");
}else if(swipe.direction == UISwipeGestureRecognizerDirectionRight){
NSLog(@"向右扫");
}
}
#pragma mark - 添加旋转+捏合手势
-(void)setUpRotation{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate = self;
[self.redView addGestureRecognizer:rotation];
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pin:)];
pin.delegate = self;
[self.redView addGestureRecognizer:pin];
}
-(void)rotation:(UIRotationGestureRecognizer *)rotation{
self.redView.transform = CGAffineTransformRotate(self.redView.transform, rotation.rotation);
rotation.rotation = 0;
}
-(void)pin:(UIPinchGestureRecognizer *)pin{
self.redView.transform = CGAffineTransformScale(self.redView.transform,pin.scale, pin.scale);
pin.scale = 1.0;
}
#pragma mark -UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
#pragma mark -UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- UILongPressGestureRecognizer(长按手势)
#pragma mark - 添加长安手势
-(void)setUpPress{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
longPress.minimumPressDuration = 1.0;
longPress.allowableMovement = 5;
[self.redView addGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer *)longPress{
NSLog(@"长按手势");
}
- UIPanGestureRecognizer(拖拽手势)
#pragma mark - 添加拖拽手势
-(void)setUpPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[self.redView addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer *)pan{
CGPoint curP = [pan translationInView:self.redView];
self.redView.transform = CGAffineTransformTranslate(self.redView.transform, curP.x, curP.y);
[pan setTranslation:CGPointZero inView:self.redView];
}