iOS开发核心动画之触摸手指识别

一.手势识别理论

1. UIGestureRecognizer手势识别器

利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势

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


2. 触摸手指类型

UITapGestureRecognizer(敲击)

UIPinchGestureRecognizer(捏合,用于缩放)

UIPanGestureRecognizer(拖拽)

UISwipeGestureRecognizer(轻扫)

UIRotationGestureRecognizer(旋转)

UILongPressGestureRecognizer(长按)


3.手势使用方法

1> 创建手势并监听

2> 添加手势

3> 实现手势方法


二. 手势识别代码

1. 点击/敲击 : UITapGestureRecognizer

1> 创建

  1. - (void)setUpTap
  2. {
  3. // 点击
  4. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
  5. [self.imageView addGestureRecognizer:tap];
  6. tap.delegate = self;
  7. }

2 > 实现方法

  1. - (void)tap:(UITapGestureRecognizer *)tap
  2. {
  3. NSLog(@"%s", __func__);
  4. }

3> 点击手势成为代理<UIGestureRecognizerDelegate>,实现shouldReceiveTouch,可设定某些范围可以点击

  1. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  2. {
  3. // 获取当前点的位置
  4. CGPoint curP = [touch locationInView:self.imageView];
  5. if (curP.x > self.imageView.bounds.size.width * 0.5) { // 右边不可接受点击事件
  6. return NO;
  7. } else { // 左边可以接受点击事件
  8. return YES;
  9. }
  10. }

2. 捏合(放大/缩小) : UIPinchGestureRecognizer

1> 对象创建

  1. // 捏合
  2. UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
  3. pinch.delegate = self;
  4. [self.imageView addGestureRecognizer:pinch];
2> 实现监听方法 缩放属性: @property (nonatomicCGFloat scale;
  1. - (void)pinch:(UIPinchGestureRecognizer *)pinch
  2. {
  3. // 捏合缩放
  4. self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
  5. // 恢复缩放
  6. [pinch setScale:1];
  7. }


3. 旋转 : UIRotationGestureRecognizer

1> 对象创建

  1. // 旋转
  2. UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
  3. rotation.delegate = self;
  4. [self.imageView addGestureRecognizer:rotation];
2> 实现监听方法 旋转属性: @property (nonatomicCGFloat rotation; 
  1. - (void)rotation:(UIRotationGestureRecognizer *)rotation
  2. {
  3. self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);


// 旋转清零

  1. [rotation setRotation:0];
  2. }

4. 拖动(移动) : UIPanGestureRecognizer

1> 对象创建

  1. // 拖动
  2. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
  3. [self.imageView addGestureRecognizer:pan];
2> 实现监听方法 注:拖动距离需要清零
  1. - (void)pan:(UIPanGestureRecognizer *)pan
  2. {
  3. // 计算拖动距离(相对于初始位置)
  4. CGPoint transP = [pan translationInView:self.imageView];
  5. self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y);
  6. // 拖动距离请0
  7. [pan setTranslation:CGPointZero inView:self.imageView];
  8. }

5. 轻扫手势 : UISwipeGestureRecognizer

1> 对象创建

  1. // 滑动 (默认向右滑)
  2. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
  3. // 设置滑动方向
  4. /*
  5. UISwipeGestureRecognizerDirectionRight = 右滑
  6. UISwipeGestureRecognizerDirectionLeft = 左滑
  7. UISwipeGestureRecognizerDirectionUp = 上滑
  8. UISwipeGestureRecognizerDirectionDown = 下滑
  9. */
  10. swipe.direction = UISwipeGestureRecognizerDirectionUp;
  11. [self.imageView addGestureRecognizer:swipe];
2> 实现监听方法
  1. - (void)swipe:(UISwipeGestureRecognizer *)swipe
  2. {
  3. NSLog(@"%s", __func__);
  4. }


6. 长按 : UILongPressGestureRecognizer

1> 对象创建

  1. //1.创建长按手势
  2. UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longP:)];
  3. //2.添加手势
  4. [self.imageV addGestureRecognizer:longP];

2> 实现监听方法

  1. //当手指长按的时候调用,注意当长按时,手指移动的时候,会持续调用这个方法,当手指离开时也会调用这个方法.
  2. - (void)longP:(UILongPressGestureRecognizer *)longP{
  3. /*
  4. UIGestureRecognizerStatePossible
  5. UIGestureRecognizerStateBegan = 开始长按
  6. UIGestureRecognizerStateChanged = 长按移动
  7. UIGestureRecognizerStateEnded = 长按结束
  8. UIGestureRecognizerStatePossible
  9. UIGestureRecognizerStateCancelled = 取消长按
  10. UIGestureRecognizerStatePossible
  11. UIGestureRecognizerStateFailed
  12. UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded = 长按结束
  13. */
  14. if (longP.state == UIGestureRecognizerStateBegan) {
  15. NSLog(@"开始长按");
  16. }else if(longP.state == UIGestureRecognizerStateChanged){
  17. NSLog(@"长按时移动");
  18. }else if(longP.state == UIGestureRecognizerStateEnded){
  19. NSLog(@"手指离开");
  20. }
  21. }

7. 旋转和捏合

1> 添加这2个手势,并设置代理

  1. [self setUpRotation];
  2. [self setUpPinch];

2> 实现代理方法允许多手势

  1. // 允许多触摸事件
  2. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  3. {
  4. return YES;
  5. }
posted @ 2015-11-27 12:50  文刂Rn  阅读(372)  评论(0编辑  收藏  举报