iOS开发之手势gesture详解(一)

前言  

  在iOS中,你可以使用系统内置的手势识别(GestureRecognizer),也可以创建自己的手势.GestureRecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象,当发生手势,绑定到的view对象会响应,它确定这个动作是否对应一个特定的手势(swipe,pinch,pan,rotation).如果它能识别这个手势,那么就会向绑定它的view发送消息,如下图

 

 UIKit框架提供了一些预定义的GestureRecognizer.包含下列手势

  •  UITapGestureRecognizer敲击手势(单击和双击)
  •  UIPanGestureRecognizer(拖动手势)
  •  UIPinchGestureRecognizer(缩放手势)
  •  UISwipeGestureRecognizer(擦碰手势)
  •  UIRotationGestureRecognizer(旋转手势)
  •  UILongPressGestureRecognizer(长按手势)

如果你想让你的应用程序来识别一个独特的手势,如选择目录或纠结的运动,你可以创建自己的自定义GestureRecognizer,将在下篇介绍

将特定的手势和view相关联

  每一个特定的手势必须关联到view对象中才会有作用,一个view对象可以关联多个不同的特定手势,但是每一个特定的手势只能与一个view相关联。当用户触摸了view,这个GestureRecognizer就会接受到消息,它可以响应特定的触摸事件。

与特定view关联

  • 创建GestureRecognizer实例
  • addGestureRecognizer
  • 实现处理手势的方法

可以使用removeGestureRecognizer: 来移除手势。

_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlerPanGesture:)];
    _panGestureRecognizer.delegate = self;
    _panGestureRecognizer.maximumNumberOfTouches = 2;
    _panGestureRecognizer.minimumNumberOfTouches = 2;
    [self.view addGestureRecognizer:_panGestureRecognizer];

- (void)handlerPanGesture:(UIPanGestureRecognizer *)recognizer
{
    if ((recognizer.state == UIGestureRecognizerStateBegan) ||
        (recognizer.state == UIGestureRecognizerStateChanged))
    {
        CGPoint offset = [recognizer translationInView:self.view];
        CGRect frame = self.rightViewController.view.frame;
        frame.origin.x += offset.x;
        if (frame.origin.x >= 0 && frame.origin.x <= kScreenWidth)
        {
            self.rightViewController.view.frame = frame;
        }
        
        [recognizer setTranslation:CGPointZero inView:self.view];
    }
    else if (recognizer.state == UIGestureRecognizerStateEnded)
    {
        BOOL isVisible = self.rightViewController.view.frame.origin.x < kScreenWidth / 2;
        [self showRightView:isVisible];
    }
}

 

手势识别状态

  

Gesture recognizers从一个状态转到另一状态(state)。对于每个状态,根据它们是否符合特定条件来决定时候可以移动到下一个状态。它们分析多点触摸。是否识别失败。未能识别手势意味着state 转换失败。UIGestureRecognizerStateFailed。详见UIGestureRecognizerState枚举

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
    UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state
    
    UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop
    UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
    UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible
    
    UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible
    
    // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled
    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible
};

 

为view添加多个手势

  当一个view添加多个手势时,在缺省情况下,没有为优先执行哪个手势做排序,每次发生不同。不过你可以覆盖默认的行为(使用类方法、委托方法、和子类化覆盖这些)

  • 指定一个Gesture recognizers应该在另一个前捕捉。

requireGestureRecognizerToFail: 这个方法就是在作为参数的Gesture recognizer失败以后接受者才发生,否则从不会发生。

[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];
  • 允许2个手势同时操作

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

  • 禁止在某一点发生Gesture recognizers
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UIControl class]])
    {
        return NO;
    }
    
    return YES;
}

指定一个单向关系两个手势识别器

想控制两个识别器相互作用,但你需要指定一个单向关系,您可以重写或canPreventGestureRecognizer:或canBePreventedByGestureRecognizer:子类方法。return yes。例如,如果你想要一个旋转的姿态来防止捏动作,但你不想夹手势防止旋转的姿态。例如,你想一个旋转手势阻止一个缩放手势,但你不想一个缩放手势阻止旋转手势,就加入下面代码

[rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];

 

转自:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html

转载于:https://www.cnblogs.com/gongchen/p/3907031.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 UIScrollView 上添加手势识别器时,有可能会出现手势冲突的情况。这通常是由于 UIScrollView 自身的手势识别器和添加的手势识别器之间的冲突引起的。 要解决这个问题,有几种可能的方法: 1. 禁用 UIScrollView 的手势识别器。这可以通过将 UIScrollView 的属性 `canCancelContentTouches` 和 `delaysContentTouches` 设置为 `NO` 来实现。但是,这样会使 UIScrollView 的滚动功能也被禁用,因此在某些情况下可能并不可取。 2. 使用手势识别器代理方法来控制手势冲突。在手势识别器代理方法 `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` 中,可以根据需要返回 `YES` 或 `NO`,以允许或禁止多个手势识别器同时识别手势。 3. 将手势识别器添加到 UIScrollView 的子视图中,而不是直接添加到 UIScrollView 上。这样,手势识别器就不会与 UIScrollView 的手势识别器冲突了。 下面是一个示例代码,演示了如何在 UIScrollView 上添加一个 UISwipeGestureRecognizer 手势识别器,并处理手势冲突的情况: ```objc // 创建一个 UISwipeGestureRecognizer 手势识别器 UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; // 设置手势方向 swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; // 将手势识别器添加到 UIScrollView 上 [scrollView addGestureRecognizer:swipeGestureRecognizer]; // 实现手势识别器代理方法,处理手势冲突 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) { return NO; // 禁止 UISwipeGestureRecognizer 和 UIScrollView 同时识别手势 } return YES; } // 处理 UISwipeGestureRecognizer 手势 - (void)handleSwipe:(UISwipeGestureRecognizer *)gestureRecognizer { // 处理手势事件 } ``` 希望这些提示可以帮助你解决 UIScrollView 上的手势冲突问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值