UIView 中的控件事件穿透


前几天公司让迭代项目的某些页面,然而在其中我发现了不可人知的事情(其实就是没有想到较好的解决办法而已),接下来我就一步一步的踏进了这个坑。

来 先上个图 给你们看一下需求
大概意思是要求上下有个叠层悬浮的感觉 当时第一个想法就是
”这么简单 直接把TableView的y大约中间偏下那个位置不就行了”

但是事实不是我想象那样的 后来经过朋友的帮助 给了一个笨笨的方法 就是加一个透明的TableHeadView 发现这样是可行的 但是问题来了 也是咱们今天要讲的主题,因为下面那层的商品展示是用SDCycleScrollView写的 但是因为上面有一层透明的View隔档着 无法对下面的SDCycleScrollView进行交互 然后经过各种寻找 找到了一个好办法 。

我们需要解决的就是怎样穿透上面的一层View能和下面的那层视图交互
第一个办法就是 把View的userInteractionEnabled 属性设置为 NO 但是需要注意的是 如果 UIView 有自己的交互事件该如何办呢?而且这个 userInteractionEnabled 不能动态设置,等到点击后决定设置它的 NO 是没用的

第二个办法就是 UIView 接受到点击事件后主动去触发下面按钮的点击,这时的问题有三,按钮没有点击过程中的交换效果、多层 UIView 时不切实际,逐层下传吗、还有就是其他双击、三击或别的手势如何处理

然后最后一个办法就是 重写hitTest方法 这个方法的大概意思是 只要实现了最顶层的 UIView 的 hitTest 方法,在某些情况返回下层的某个按钮实例,即相当于把那个按钮的事件透出来了,比如在点击落在该按钮上时,不管这个按钮在 UIView 下多少层都可以把它挖出来。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system

请看下面的效果图

三个图的意思分别是:

  1. 所见到的,按钮被半透明红色 View 遮住了一部分
  2. 可点击未遮住的按钮部分,可看到按钮被点下未抬起的效果
  3. 在红色的 View 中点击按钮被遮住部分,同样触发了按钮的相应事件,且有中间效果,也就是说按钮穿透出来了

再看代码

#import "ViewController.h"
#import "CustomView.h"
 
@implementation ViewController{
    UIButton *passthroughButton;
}
 
#pragma mark - View lifecycle
 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    passthroughButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [passthroughButton setTitle:@"Passthrough" forState:UIControlStateNormal];
    [self.view addSubview:passthroughButton];
    passthroughButton.frame = CGRectMake(20, 50, 120, 28);
     
    CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(80, 10, 300, 200)];
    customView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.5];
    customView.passthroughViews = [NSArray arrayWithObject:passthroughButton];
    [self.view addSubview:customView];

} 

//
//  CustomView.h
//  TestPopover

#import <UIKit/UIKit.h>
 
@interface CustomView : UIView
@property (nonatomic, copy) NSArray *passthroughViews;
@end

#import "CustomView.h"
 
@interface CustomView()
-(BOOL) isPassthroughView: (UIView *) view;
@end
 
@implementation CustomView{
    BOOL testHits; 
}
 
@synthesize passthroughViews=_passthroughViews;
 
- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if(testHits){
        return nil;
    }
     
    if(!self.passthroughViews
        || (self.passthroughViews && self.passthroughViews.count == 0)){
        return self;
    } else {
         
        UIView *hitView = [super hitTest:point withEvent:event];
         
        if (hitView == self) {
            //Test whether any of the passthrough views would handle this touch
            testHits = YES;
            CGPoint superPoint = [self.superview convertPoint:point fromView:self];
            UIView *superHitView = [self.superview hitTest:superPoint withEvent:event];
            testHits = NO;
             
            if ([self isPassthroughView:superHitView]) {
                hitView = superHitView;
            }
        }
         
        return hitView;
    }
}
 
- (BOOL)isPassthroughView:(UIView *)view {
     
    if (view == nil) {
        return NO;
    }
     
    if ([self.passthroughViews containsObject:view]) {
        return YES;
    }
     
    return [self isPassthroughView:view.superview];
}             
 
- (void) dealloc {
    self.passthroughViews = nil;
}
 
@end

好了 大概意思就是这样的 
在此非常感谢这篇文章给我的帮助 希望能给遇到类似问题的同学一些帮助
https://unmi.cc/uiview-event-passthrough/

还有 关键要理解 hitTest 方法的作用,可参考这两篇文章:
1. http://blog.sina.com.cn/s/blog_677089db01012wpg.html
2. [https://github.com/werner77/WEPopover](https://github.com/werner77/WEPopover)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容