1. 程式人生 > >iOS 子檢視超出父檢視不響應解決辦法

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就可以了