父视图中重写该方法
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