通过touches方法监听view触摸事件,有很明显的几个缺点
- 必须得自定义view
- 由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
- 不容易区分用户的具体手势行为
iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度
UIGestureRecognizer(抽象类)
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
- UITapGestureRecognizer(敲击)
- UIPinchGestureRecognizer(捏合,用于缩放)
- UIPanGestureRecognizer(拖拽)
- UISwipeGestureRecognizer(轻扫)
- UIRotationGestureRecognizer(旋转)
- UILongPressGestureRecognizer(长按)
UITapGestureRecognizer(敲击)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| - (void)setUpTap { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tap.delegate = self; [self.imageView addGestureRecognizer:tap]; }
- (void)pan:(UIPanGestureRecognizer *)pan { CGPoint curP = [pan locationInView:self.imageView]; CGPoint transP = [pan translationInView:self.imageView]; self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y); [pan setTranslation:CGPointZero inView:self.imageView]; NSLog(@"%@",NSStringFromCGPoint(curP)); }
|
手势代理方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return NO; }
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { 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; } }
|
UILongPressGestureRecognizer(长按)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| - (void)setUpLongPress { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.imageView addGestureRecognizer:longPress]; }
- (void)longPress:(UILongPressGestureRecognizer *)longPress { if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"%s",__func__); } }
|
UISwipeGestureRecognizer(轻扫)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.imageView addGestureRecognizer:swipe];
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self.imageView addGestureRecognizer:swipeDown];
- (void)swipe { NSLog(@"%s",__func__); }
|
UIRotationGestureRecognizer(旋转)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| - (void)setUpRotation { UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; rotation.delegate = self; [self.imageView addGestureRecognizer:rotation]; }
- (void)rotation:(UIRotationGestureRecognizer *)rotation { NSLog(@"%f",rotation.rotation); self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation); rotation.rotation = 0; }
|
UIPinchGestureRecognizer(捏合)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| - (void)setUpPinch { UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; pinch.delegate = self; [self.imageView addGestureRecognizer:pinch]; }
- (void)pinch:(UIPinchGestureRecognizer *)pinch { self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); pinch.scale = 1; }
|
UIPanGestureRecognizer(拖拽)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| - (void)setUpPan { UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.imageView addGestureRecognizer:pan]; }
- (void)pan:(UIPanGestureRecognizer *)pan { CGPoint curP = [pan locationInView:self.imageView]; CGPoint transP = [pan translationInView:self.imageView]; self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y); [pan setTranslation:CGPointZero inView:self.imageView]; NSLog(@"%@",NSStringFromCGPoint(curP)); }
|
练习:抽屉效果
代码:抽屉效果