Gesture Recognizer

Gesture Recognizer

以前想监听一个view上面的触摸事件,之前的做法是

1 ->自定义一个view

2 ->实现view的touches方法,在方法内部实现具体处理代码

3 ->通过touches方法监听view触摸事件,有很明显的几个缺点

4 ->必须得自定义view 弊端:由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件,不容易区分用户的具体手势行为

UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势

UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)


UIGestureRecognizer的方法

#import"ViewController.h"@interfaceViewController()@property(weak,nonatomic)IBOutletUIImageView*imageView;

@end


@implementationViewController- (void)viewDidLoad {    [superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.// 同时做缩放和旋转// 拖动UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(pan:)];    [_imageView addGestureRecognizer:pan];}// 拖动方法- (void)pan:(UIPanGestureRecognizer*)pan{// 获取手势偏移量CGPointtransP = [pan translationInView:_imageView];// 修改控件位置,transform平移_imageView.transform=CGAffineTransformTranslate(_imageView.transform, transP.x, transP.y);// 复位[pan setTranslation:CGPointZeroinView:_imageView];}

- (void)setUpPinch{//捏合UIPinchGestureRecognizer*pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinch:)];    pinch.delegate=self;    [_imageView addGestureRecognizer:pinch];}

#pragma mark - 手势代理//  同时做缩放和旋转// Simultaneously:同时// 是否允许同时支持多个手势- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer{returnYES;}

// 触发捏合手势的时候就会调用- (void)pinch:(UIPinchGestureRecognizer*)pinch{// 获取缩放比例// 缩放图片_imageView.transform=CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);// 复位pinch.scale=1;}- (void)setUpRotation{// 旋转手势UIRotationGestureRecognizer*rotation = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotation:)];    rotation.delegate=self;    [_imageView addGestureRecognizer:rotation];}

// 触发旋转手势就会调用- (void)rotation:(UIRotationGestureRecognizer*)rotation{// 旋转图片// transform:形变(平移,旋转,缩放)// CGAffineTransformMakeRotation:相对于最原始的值去旋转// make会把之前的值清空,重新开始_imageView.transform=CGAffineTransformRotate(_imageView.transform, rotation.rotation);// 复位:表示相对于上一次rotation.rotation=0;NSLog(@"%f",rotation.rotation);}#pragma mark - 添加轻扫手势- (void)setUpSwipe{// 默认一个手势只支持一个方向// 轻扫UISwipeGestureRecognizer*swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe)];    swipe.direction=UISwipeGestureRecognizerDirectionLeft;    [_imageView addGestureRecognizer:swipe];// 添加右边轻扫手势UISwipeGestureRecognizer*swipeR = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipe)];    [_imageView addGestureRecognizer:swipeR];// 一个控件支持多个手势}// 触发轻扫手势- (void)swipe{NSLog(@"%s",__func__);}// 添加长按手势- (void)setUpLongPress{// 长按UILongPressGestureRecognizer*longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];    [_imageView addGestureRecognizer:longPress];}

// 当长按开始触发和手指抬起或者手指移动都会调用- (void)longPress:(UILongPressGestureRecognizer*)longPress{// 在开始长按的时候做事情if(longPress.state==UIGestureRecognizerStateBegan) {NSLog(@"%s",__func__);    }}#pragma mark - UIGestureRecognizerDelegate

// 是否允许接收手指//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch//{//    // 获取当前的触摸点//    CGPoint curP = [touch locationInView:self.imageView];//    // 左边可以点击,右边不允许点击//    if (curP.x < self.imageView.bounds.size.width * 0.5) {//        return YES;//    }else{////        return NO;//    }////}

// 添加点按手势- (void)setUpTap{// 点按UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tap)];    tap.delegate=self;    [_imageView addGestureRecognizer:tap];}// 只要触发点按手势,就会调用- (void)tap{NSLog(@"%s",__func__);}@end

你可能感兴趣的:(Gesture Recognizer)