iOS 子视图超出父视图不响应解决办法

父视图中重写该方法

Objective-C

- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event {
    UIView * view = [super hitTest:point withEvent:event];
    if (view == nil) {
        for (UIView * subView in self.subviews) {
            // 将坐标系转化为自己的坐标系
            CGPoint tp = [subView convertPoint:point fromView:self];
            if (CGRectContainsPoint(subView.bounds, tp)) {
                view = subView;
            }
        }
    }
    return view;
}

Swift

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        var view = super.hitTest(point, withEvent: event)
        if view == nil {
            for subView in self.subviews {
                let tp = subView.convertPoint(point, fromView: self)
                if CGRectContainsPoint(subView.bounds, tp) {
                    view = subView
                }
            }
        }
        return view
    }

测试代码

   override func viewDidLoad() {
        super.viewDidLoad()
       let costemView = CustomView(frame: CGRectMake(100, 100 , 100, 100))
        self.view.addSubview(costemView)
        
        let button = UIButton(frame: CGRectMake(-20, -20, 40, 40))
        costemView.addSubview(button)
        button.backgroundColor = UIColor.lightGrayColor()
        button.addTarget(self, action: #selector(aaa), forControlEvents: .TouchUpInside)
    }
    
    func aaa() -> Void {
        print("dafd")
    }

在父试图里面处理但是这样处理不太优雅

这里有更优雅的处理方法

UIView+Chain.h

@interface UIView (Chain)

@property (nonatomic, assign) BOOL ableRespose;

@end

UIView+Chain.m

@implementation UIView (Chain)

+ (void)load {
    Class class = self;
    Method originMethod = class_getInstanceMethod(class, @selector(hitTest:withEvent:));
    Method targetMethod = class_getInstanceMethod(class, @selector(exchange_hitTest:withEvent:));
    if (!originMethod || !targetMethod) {
        NSLog(@"交换失败");
        return;
    }
    BOOL didAddMethod = class_addMethod(class,@selector(hitTest:withEvent:), method_getImplementation(targetMethod), method_getTypeEncoding(targetMethod));

    if (didAddMethod) {
        class_replaceMethod(class,@selector(exchange_hitTest:withEvent:), method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    } else {
        method_exchangeImplementations(originMethod, targetMethod);
    }
}

- (UIView *)exchange_hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView * view = [self exchange_hitTest:point withEvent:event];
    if (view) {
        return view;
    } else {
        for (UIView * v in self.subviews) {
            if (v.ableRespose) {
                if (CGRectContainsPoint(v.frame, point)) {
                    return v;
                }
            }
        }
        return nil;
    }
}

- (void)setAbleRespose:(BOOL)ableRespose {
    objc_setAssociatedObject(self, @selector(ableRespose), @(ableRespose), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)ableRespose {
    return objc_getAssociatedObject(self, _cmd) != nil ? [objc_getAssociatedObject(self, _cmd) boolValue] : NO;
}

@end

只需要设置超出范围的视图的ableRespose属性为YES就可以了

工具链接 https://github.com/BetrayalPromise/MineCommonUtils 希望各位大佬赏赐一颗start

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值