- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

UIApplication管理事件队列,当事件发生时,UIApplication把事件依次向下传,直到找到最适合的控件未知。

控件是通过- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event来寻找的。比如如果这个方法返回self,则不会再往下找了。。。

 

UIApplication -> [UIWindow hitTest:withEvent:] -> subView hitTest:withEvent........

判断原则:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    
    // 1.判断当前控件能否接收事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;
    
    // 2. 判断点在不在当前控件
    if ([self pointInside:point withEvent:event] == NO) return nil;
    
    // 3.从后往前遍历自己的子控件
    NSInteger count = self.subviews.count;
    
    for (NSInteger i = count - 1; i >= 0; i--) {
        UIView *childView = self.subviews[i];
        
        // 把当前控件上的坐标系转换成子控件上的坐标系
        CGPoint childP = [self convertPoint:point toView:childView];
        
        UIView *fitView = [childView hitTest:childP withEvent:event];
        
        
        if (fitView) { // 寻找到最合适的view
            return fitView;
        }
    }
    // 循环结束,表示没有比自己更合适的view
    return self;
}

 

转载于:https://www.cnblogs.com/yintingting/p/4552277.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,可以使用以下代码实现: 1. 在UITableViewCell的初始化方法中,创建YYLabel并添加到cell.contentView中: ``` - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectZero]; [self.contentView addSubview:label]; self.yyLabel = label; } return self; } ``` 2. 在设置UITableViewCell的数据源方法中,设置YYLabel的frame和内容: ``` - (void)setModel:(Model *)model { _model = model; self.yyLabel.frame = CGRectMake(10, 10, self.contentView.bounds.size.width - 20, model.textLayout.textBoundingSize.height); self.yyLabel.attributedText = model.textLayout.text; } ``` 3. 在NSMutableAttributedString中插入attachment时,可以使用YYTextAttachment对象,并将UITextView添加到attachment中: ``` YYTextAttachment *attachment = [YYTextAttachment new]; attachment.content = textView; attachment.contentMode = UIViewContentModeScaleAspectFit; attachment.contentInsets = UIEdgeInsetsMake(, , , ); NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; [attributedString insertAttributedString:attachmentString atIndex:insertIndex]; ``` 4. 处理UITextView与TableView滚动冲突的问题,可以在UITextView的父视图中重写以下方法: ``` - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if ([self pointInside:point withEvent:event]) { self.superview.userInteractionEnabled = NO; } else { self.superview.userInteractionEnabled = YES; } return [super hitTest:point withEvent:event]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { self.superview.userInteractionEnabled = YES; [super touchesEnded:touches withEvent:event]; } ``` 希望以上代码对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值