两个view覆盖了, 在同一层级。 怎样让遮挡的view透传,让后面被盖住的view响应?
第一种方法:
重写他们的共同supview的hittest方法。
1, 当point在对应view上,返回 这个view。 这个方法太局限了,这个view的子view无法响应。
2, 当point在对应的view上,返回 这个view的hittest。 很明显这个方法更好!
第二种方法:
重写遮挡view的pointInside方法,让其在遮挡区域的点返回NO。被盖住的view自然就响应了,子view也可以响应。
@interface MycustomUIView : UIView
@property (weak, nonatomic) UIButton *button;
@property(weak,nonatomic)UIView*view2;
@end
#import "MycustomUIView.h"
@implementation MycustomUIView
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
// if( CGRectContainsPoint(self.button.bounds, [self convertPoint:point toView:self.button]) ){
// return self.button;
// }
// if( CGRectContainsPoint(self.button.bounds, [self convertPoint:point toView:self.button]) ){
// return [self.button hitTest:[self convertPoint:point toView:self.button] withEvent:event];
// }
// if (CGRectContainsPoint(self.view2.bounds, [self convertPoint:point toView:self.view2])) {
// return [self.view2 hitTest:[self convertPoint:point toView:self.view2] withEvent:event];
// }
return [super hitTest:point withEvent:event];
}
//-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
// if (CGRectContainsPoint(self.view2.bounds, [self convertPoint:point toView:self.view2])) {
// return NO;
// }
// return [super pointInside:point withEvent:event];
//}
@end
@interface MycustomView2 : UIView
@property (assign, nonatomic) CGRect passthroughRect;
@end
#import "MycustomView2.h"
@implementation MycustomView2
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
if (CGRectContainsPoint(_passthroughRect, point)){
return NO;
}
return [super pointInside:point withEvent:event];
}
@end
@interface ResponderViewController ()
@property (weak, nonatomic) IBOutlet UIButton *bottomButton;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet MycustomUIView *mycustomView;
@property (weak, nonatomic) IBOutlet MycustomView2 *zedangView;
@end
@implementation ResponderViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mycustomView.button=self.bottomButton;
self.mycustomView.view2=self.view2;
self.zedangView.passthroughRect=[self.view convertRect: self.view2.frame toView:self.zedangView] ;
}
- (IBAction)bottomButton2Action:(id)sender {
NSLog(@" 😊 %s ",__func__);
}
- (IBAction)bototomButtonAction:(id)sender {
NSLog(@" 😊 %s ",__func__);
}
- (IBAction)outterbuttonAction:(id)sender {
NSLog(@" 😊 %s ",__func__);
}
@end
网友评论