美文网首页
hitTest响应分析

hitTest响应分析

作者: 我是小胡胡123 | 来源:发表于2018-06-11 17:29 被阅读8次

两个view覆盖了, 在同一层级。 怎样让遮挡的view透传,让后面被盖住的view响应?
第一种方法:
重写他们的共同supview的hittest方法。

1, 当point在对应view上,返回 这个view。 这个方法太局限了,这个view的子view无法响应。
2, 当point在对应的view上,返回 这个view的hittest。 很明显这个方法更好!

第二种方法:
重写遮挡view的pointInside方法,让其在遮挡区域的点返回NO。被盖住的view自然就响应了,子view也可以响应。

image.png
@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

相关文章

  • hitTest响应分析

    两个view覆盖了, 在同一层级。 怎样让遮挡的view透传,让后面被盖住的view响应?第一种方法:重写他们的共...

  • ARkit -- hitTest(_ point:, types

    HitTest 关于HitTest有三个方法 方法一 : hitTest(_ point:, types:) 关于...

  • 事件的传递

    事件的传递 hitTest的方法 hitTest:是控件方法 hitTest作用:就是帮你寻找最合适的View h...

  • 让UIView、UIWindow等透明遮挡不影响下面视图操作方法

    重写view的hitTest方法- (UIView *)hitTest:(CGPoint)point withEv...

  • hitTest由浅入深

    本文将从如下几个方面来介绍它: 什么是hitTest hitTest、响应链和手势的先后顺序是什么 hitTest...

  • 第二篇:CALayer能力之hitTest响应事件

    目录 一、CALayer hitTest响应事件 二、UIView hitTest+pointInside判断点击...

  • iOS的pointInside和hitTest方法

    pointInside和hitTest区别: hitTest和pointInside是UIView提供的触摸事件处...

  • hitTest

    背景 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中,UIApplicati...

  • hitTest

    熟记响应者链没大毛用,主要还是要从代码层面去理解其过程。从图记起: 以点击E为例Hittest步骤如下:1.触摸点...

  • hitTest

    按需求屏蔽事件,默认是返回[super hitTest:point withEvent:event],若想某个区域...

网友评论

      本文标题:hitTest响应分析

      本文链接:https://www.haomeiwen.com/subject/orbheftx.html