iOS手势与响应者链

96
秦明Qinmin
2017.10.30 07:03* 字数 1055

这篇文章侧重于讲解手势识别、手势冲突、以及手势与响应链的关系。在处理多个手势识别场景,很容易出现手势冲突的问题,比如,我们需要对图片进行单击、长按、旋转、缩放、拖动、翻转等操作的时候,我们需要使用各种手势来完成相应的操作,这些手势组合在一起很容易出现手势冲突,而且当引入手势识别的时候,有时候你还会发现响应者链似乎不符合规律了,这些都是处理手势识别容易遇到的。

手势

手势识别在 iOS 中非常重要,它极大地丰富了用户与iOS程序的交互方式。同时iOS提供的手势API非常简洁,在使用时一般的步骤为:1)创建手势实例对象,2)添加手势到需要识别的UIView中,注意每个手势只对应一个UIView。在 iOS 系统中主要有以下常见的手势:

UIPanGestureRecognizer(拖动)

UIPinchGestureRecognizer(捏合)

UIRotationGestureRecognizer(旋转)

UITapGestureRecognizer(点按)

UILongPressGestureRecognizer(长按)

​UISwipeGestureRecognizer(轻扫)

UIScreenEdgePanGestureRecognizer (边缘手势)

响应链

响应者链是系统寻找事件响应者的路径。该路径开始于firstResponder结束于单例application。事件首先会让firstResponder对象去处理,如果它无法处理则会向其nextResponder对象转发事件。当所有对象都无法处理事件后将最后转发到application处并最终忽略该事件。在UIKit中,UIApplication、UIView、UIViewController这几个类都是直接继承自UIResponder类。因此UIKit中的视图、控件、视图控制器,以及我们自定义的视图及视图控制器都有响应事件的能力。这些对象通常被称为响应对象,是响应链中的一个节点。

响应者链png

手势冲突

  • 1、如果一个手势A的识别部分是另一个手势B的子部分时,默认情况下A就会先识别,B就无法识别了。我们可以指定某个手势执行的前提是另一个手势失败才会识别执行,这样控制手势识别的响应顺序。
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
  • 2、如果同一视图需要一次响应多个手势操作,可以实现下面的UIGestureRecognizerDelegate的代理方法,当返回YES的时候,可以同时响应多个手势。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

同时响应例子:

- (void)test1OnView:(UIView *)view
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    tap.delegate = self;
    [view addGestureRecognizer:tap];

    UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    press.delegate = self;
    [view addGestureRecognizer:press];
    
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    swip.delegate = self;
    [view addGestureRecognizer:swip];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    pan.delegate = self;
    [view addGestureRecognizer:pan];
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    rotation.delegate = self;
    [view addGestureRecognizer:rotation];
    
}

- (void)onGestureRecognizerTrigger:(UIGestureRecognizer *)gestureRecognizer
{
    NSLog(@"%@", [gestureRecognizer class]);
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

可以看到拖动的时候,还识别了轻扫手势:

手势识别[5246:277106] UIPanGestureRecognizer
手势识别[5246:277106] UISwipeGestureRecognizer
手势识别[5246:277106] UIPanGestureRecognizer
手势识别[5246:277106] UIPanGestureRecognizer
手势识别[5246:277106] UIPanGestureRecognizer
  • 3、父视图如果有手势需要识别,子视图同样有相似触摸事件需要处理,这时候就可能产生冲突。我们可以实现 UIGestureRecognizerDelegate 的以下代理方法来解决相关冲突:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

在UIView中添加UITableView,当UIView需要识别轻敲手势的时候,这时UITableViewCell 点击便失效,我们通过实现- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch代理方法,让 UITableViewCell 可以被点击。类似地在UIScrollView中添加 UITableView、UICollectionView 时,UITableViewCell不能响应相关点击的时候,也可以通过这种方法解决。

- (void)test2OnView:(UIView *)view
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    tap.delegate = self;
    tap.delegate = self;
    [view addGestureRecognizer:tap];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:view.bounds];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TableViewCellReuseIdentifier];
    [view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellReuseIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"row = %ld", indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAt index = %ld", indexPath.row);
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[UITableView class]]) {
        return NO;
    }
    
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    
    return YES;
}

手势与响应者链

1、手势与响应者链有一些差别,触摸事件首先会传递到手势上,如果手势识别成功,就会取消事件的继续传递。如果手势识别失败,事件才会被响应链处理。例子见 手势冲突 的第三点。

2、对于 UIButton,UISwitch,UISegmentedControl,UIStepper、UIPageControl 进行单击操作,如果父视图有轻敲手势需要识别,依然会按照响应链来处理,先响应这些控件的单击事件,这仅适用于与控件的默认操作重叠的手势识别。

- (void)test3OnView:(UIView *)view
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    [view addGestureRecognizer:tap];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
    button.backgroundColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
}

- (void)buttonTapped:(UIButton *)button
{
    NSLog(@"%@", [button class]);
}

输出结果为:

手势识别[6618:352925] UIButton

3、如果子视图和父视图都有手势需要识别,则按照firstResponder从子视图到父视图传递。

- (void)test4OnView:(UIView *)view
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger:)];
    [view addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onGestureRecognizerTrigger1:)];
    UIView *button = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
    button.backgroundColor = [UIColor orangeColor];
    [button addGestureRecognizer:tap1];
    
    [view addSubview:button];
}

- (void)onGestureRecognizerTrigger1:(UIGestureRecognizer *)gestureRecognizer
{
    NSLog(@"%s", __func__);
}

输出结果为:

手势识别[6680:355254] -[ViewController onGestureRecognizerTrigger1:]
博客
Gupao