代码改变世界

控制局部对触摸的响应与否

2019-06-18 11:47  法子  阅读(266)  评论(0编辑  收藏  举报

BView遮挡AView,BView有子viewB1,AView有手势。希望点击在B1中的时候,AView可以响应手势。

若果让BView整个userInteractionEnabled = NO,那么点击在BView的任何地方,AView的手势都会响应,不符合要求。

可以用一下函数实现

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event;   // default returns YES if point is in bounds

 

@interface BView ()
@property (strong, nonatomic) UIView *B1;

@end

@implementation BView

...

//重写pointInside:withEvent函数
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self.B1 pointInside:[self convertPoint:point toView:self.B1] withEvent:event]) { //如果点击在B1内,就不再传递,让出响应
        return NO;
    } return YES;
}
@end