UIView 中的控件事件穿透 Passthrough 的实现

本文探讨了在iOS应用中,当一个按钮被一个UIView遮盖时,如何在点击最上层UIView时触发按钮事件的问题。通过实现UIView的hitTest方法,将事件从下层控件透传到上层需要响应事件的控件,从而解决了多层控件间的点击事件冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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


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

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

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

有两部分代码,分别是 ViewController 和  CustomController

01 //
02 //  ViewController.h
03   
04 //
05 //  Created by Unmi on 11/15/11.
06 //  Copyright (c) 2011 http://unmi.cc. All rights reserved.
07 //
08   
09 #import <UIKit/UIKit.h>
10   
11 @interface ViewController : UIViewController{
12 }
13  <a href="http://my.oschina.net/u/567204" target="_blank"rel="nofollow">@end</a>
14  
15 //
16 //  ViewController.m
17 //
18 //  Created by Unmi on 11/15/11.
19 //  Copyright (c) 2011 http://unmi.cc. All rights reserved.
20 //
21   
22 #import "ViewController.h"
23 #import "CustomView.h"
24   
25 @implementation ViewController{
26     UIButton *passthroughButton;
27 }
28   
29 #pragma mark - View lifecycle
30   
31 - (void)viewDidLoad
32 {
33     [super viewDidLoad];
34   
35     passthroughButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
36     [passthroughButton setTitle:@"Passthrough" forState:UIControlStateNormal];
37     [self.view addSubview:passthroughButton];
38     passthroughButton.frame = CGRectMake(20, 50, 120, 28);
39     [passthroughButton release];
40       
41     CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(80, 10, 300, 200)];
42     customView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:.5];
43     customView.passthroughViews = [NSArray arrayWithObject:passthroughButton];
44     [self.view addSubview:customView];
45     [customView release];
46 }
47   
48 - (void)dealloc {
49     [super dealloc];
50 }<a href="http://my.oschina.net/u/567204" target="_blank" rel="nofollow">@end</a>

01 //
02 //  CustomView.h
03 //  TestPopover
04 //
05 //  Created by Unmi on 2/19/12.
06 //  Copyright (c) 2012 http://unmi.cc. All rights reserved.
07 //
08   
09 #import <UIKit/UIKit.h>
10   
11 @interface CustomView : UIView
12 @property (nonatomic, copy) NSArray *passthroughViews;<a href="http://my.oschina.net/u/567204"target="_blank" rel="nofollow">@end</a>
13 //
14 //  CustomView.m
15 //
16 //  Created by Unmi on 2/19/12.
17 //  Copyright (c) 2012 http://unmi.cc. All rights reserved.
18 //
19   
20 #import "CustomView.h"
21   
22 @interface CustomView()
23 -(BOOL) isPassthroughView: (UIView*) view;<a href="http://my.oschina.net/u/567204"target="_blank" rel="nofollow">@end</a>
24   
25 @implementation CustomView{
26     BOOLtestHits;
27 }
28   
29 @synthesize passthroughViews=_passthroughViews;
30   
31 -(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event{
32     if(testHits){
33         return nil;
34     }
35       
36     if(!self.passthroughViews
37         || (self.passthroughViews && self.passthroughViews.count == 0)){
38         return self;
39     else {
40           
41         UIView *hitView = [super hitTest:point withEvent:event];
42           
43         if (hitView == self) {
44             //Test whether any of the passthrough views would handle this touch
45             testHits = YES;
46             CGPoint superPoint = [self.superview convertPoint:point fromView:self];
47             UIView *superHitView = [self.superview hitTest:superPoint withEvent:event];
48             testHits = NO;
49               
50             if ([self isPassthroughView:superHitView]) {
51                 hitView = superHitView;
52             }
53         }
54           
55         return hitView;
56     }
57 }
58   
59 - (BOOL)isPassthroughView:(UIView *)view {
60       
61     if (view == nil) {
62         return NO;
63     }
64       
65     if ([self.passthroughViews containsObject:view]) {
66         return YES;
67     }
68       
69     return [self isPassthroughView:view.superview];
70 }            
71   
72 -(void) dealloc{
73     self.passthroughViews = nil;
74 }
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值