uiview点击事件穿透

我们在有多个 UIView 层叠时,比如一个按钮被一个 UIView 遮盖时,想要在点击最上层的 UIView 时能触发按钮的相应事件,我们该如何实现呢,初步可以想到几种办法:

1. 把按钮上层的所有 UIView 的 userInteractionEnabled 属性设置为 NO,要是 UIView 有自己的交互事件该如何办呢?而且这个 userInteractionEnabled 不能动态设置,等到点击后决定设置它的 NO 是没用的

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

我也一直被前面两种方式纠缠着,同时也让 UIPopoverController 的 NSArray *passthroughViews 属性提醒着,因为对于 UIPopoverController,设置到它的 passthoughViews 属性中的控件事件可以完全从 UIDimmingView 下透出来。但苦于不可能看到 UIPopoverController 的源码,还是后面一而再的 Google 终于发现了 UIView 的方法:

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

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

先看效果图:

 

三个图分别是:

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

再看代码实现,有两部分代码,分别是 ViewController 和  CustomController

ViewController.h

//
//  ViewController.h

//
//  Created by Unmi on 11/15/11.
//  Copyright (c) 2011 http://unmi.cc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
}

@end

ViewController.h

//
//  ViewController.m
//
//  Created by Unmi on 11/15/11.
//  Copyright (c) 2011 http://unmi.cc. All rights reserved.
//

#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);
    [passthroughButton release];
    
    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 release];
}

- (void)dealloc {
    [super dealloc];
}
@end

CustomView.h

//
//  CustomView.h
//  TestPopover
//
//  Created by Unmi on 2/19/12.
//  Copyright (c) 2012 http://unmi.cc. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomView : UIView
@property (nonatomic, copy) NSArray *passthroughViews;
@end

CustomView.m

//
//  CustomView.m
//
//  Created by Unmi on 2/19/12.
//  Copyright (c) 2012 http://unmi.cc. All rights reserved.
//

#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

关键要理解 hitTest 方法的作用,可参考:

1. http://blog.sina.com.cn/s/blog_677089db01012wpg.html 
2. https://github.com/werner77/WEPopover

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值