iOS事件传递及处理

https://www.jianshu.com/p/2e074db792ba

以上是最详细的博客,讲的很细致,不再赘述。一下为精华补充部分

UIView继承与UIResponder,UIResponder提供四个处理方法(PS:不使用父类处理,【super之类的,会拦截事件)

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchBegan");
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchMoved");
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchEnded");
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchCancelled");
    [super touchesCancelled:touches withEvent:event];
}


UIView进行事件分发处理时,两个主要的方法

//此方法用于确定该事件是不是本View内部,也可以人工篡改不拦截事件。(一个view能处理事件的充分必要条件是以下两个都是yes,而且view.userInterfaceEnable = YES 才可以)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return YES;
}

//此方法用来确定本View的各个子View中,哪个处理event合适,如果没有合适的子view,就会返回本类

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    NSArray * views = self.subviews;
    for (UIView * view in views) {
        if ([view isKindOfClass:[ButtonC class]]) {
            return view;
        }
    }
    return nil;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值