iOS 事件传递底层实现代码

184 阅读1分钟
//
//  LSLWindow.m
//  iOS事件传递
//
//  Created by Jason on 2019/3/12.
//  Copyright © 2019 友邦创新资讯. All rights reserved.
//

#import "LSLWindow.h"

@implementation LSLWindow

//实现底层代码
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {
        return nil;
    }
    if (![self pointInside:point withEvent:event]) {
        return nil;
    }
    
    int count = (int)self.subviews.count;
    for (int i = count - 1; i>= 0; i--) {
        //获取子控件
        UIView *childView = self.subviews[i];
        //把当前子控件的坐标系转换成子控件的坐标系
       CGPoint childPoint = [self convertPoint:point toView:childView];
       UIView *fitView = [childView hitTest:childPoint withEvent:event];
        if (fitView) {
            return fitView;
        }
    }
    return self;
    
}

@end