美文网首页
如何精确响应手势识别位置

如何精确响应手势识别位置

作者: 小苗晓雪 | 来源:发表于2017-06-19 11:21 被阅读86次

子视图并不响应 tap 点击触摸手势 , 而是至于父视图响应 tap 触摸手势的效果 , 精确定位触摸的位置:

手势触摸位置识别

1.从代理方法能否开始过滤手势触摸的位置:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
2.从手势能否进入 Possible 状态就开始过滤手势触摸的位置:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

//
//  GestureConditionViewController.m
//  15-手势实践
//
//  Created by miaodong on 2017/6/19.
//  Copyright © 2017年 miaodong. All rights reserved.
//

#import "GestureConditionViewController.h"

@interface GestureConditionViewController () <UIGestureRecognizerDelegate> {
    UIView *_superView;
    UIView *_subView;
    UITapGestureRecognizer *_tap;
}

@end

@implementation GestureConditionViewController
#pragma mark - life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    _superView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    _superView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_superView];
    
    _subView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    _subView.backgroundColor = [UIColor orangeColor];
    [_superView addSubview:_subView];
    
    _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    _tap.delegate = self;
    [_superView addGestureRecognizer:_tap];
    
}


#pragma mark - 手势方法
- (void)tap:(UITapGestureRecognizer *)tapGesture {
    [self alertMessage:@"tap"];
}

- (void)alertMessage:(NSString *)message {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"AlertVC" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alertVC animated:YES completion:nil];
}

//方法一:
#pragma mark - <UIGestureRecognizerDelegate>
//让父视图响应这个 tap 手势,子视图不响应这个手势:
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    if (gestureRecognizer == _tap) {
//        CGPoint location = [gestureRecognizer locationInView:_superView];
//        //如果在第一个参数方块里包含了第二个参数的点坐标:
//        if (CGRectContainsPoint(_subView.frame, location)) {
//            return NO;
//        }
//        return YES;
//    }
//    return YES;
//}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (gestureRecognizer == _tap) {
        //如果触摸位置所在的视图不是父视图 , 并且 触摸位置所在的视图是父视图的子孙视图:
//        if (touch.view != _superView && [touch.view isDescendantOfView:_superView]) {
//            //那么就不响应手势:
//            return NO;
//        }
        
        //用下面这种判断也是可以的~因为子视图是添加到父视图上的,触摸点击子视图位置的时候是 摸不到 父视图上的点的!
        if (!(touch.view == _superView)) {
            return NO;
        }
        return YES;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

愿编程让这个世界更美好

相关文章

网友评论

      本文标题:如何精确响应手势识别位置

      本文链接:https://www.haomeiwen.com/subject/zyjgqxtx.html