美文网首页
iOS 按钮超过父视图范围无法响应事件的处理方法

iOS 按钮超过父视图范围无法响应事件的处理方法

作者: 小六玩的并不六 | 来源:发表于2019-08-27 09:26 被阅读0次

当存在view时才会传递对应的event,现在点击了父视图以外的范围,自然返回的是nil。所以当子视图(比如按钮btn)因为一些原因超出了父视图范围,就要重写hittest方法,让其返回对应的子视图,来接收事件。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {  
    UIView *view = [super hitTest:point withEvent:event];  
    if (view == nil) {  
    CGPoint tempoint = [btn convertPoint:point fromView:self];  
        if (CGRectContainsPoint(btn.bounds, tempoint))   
    {  
           view = btn;  
        }  
     }             
    return view;  
}  

相关文章

网友评论

      本文标题: iOS 按钮超过父视图范围无法响应事件的处理方法

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