UIView 中的控件事件穿透 Passthrough 的实现
来源:互联网 发布:退乎什么意思 编辑:程序博客网 时间:2024/04/28 11:18
原文
我们在有多个 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
#import <UIKit/UIKit.h> @interfaceViewController : UIViewController{} @end
#import "ViewController.h"#import "CustomView.h" @implementationViewController{ UIButton*passthroughButton;} #pragma mark - View lifecycle - (void)viewDidLoad{ [superviewDidLoad]; passthroughButton = [UIButtonbuttonWithType: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 = [UIColorcolorWithRed:1 green:0 blue:0 alpha:.5]; customView.passthroughViews = [NSArrayarrayWithObject:passthroughButton]; [self.view addSubview:customView]; [customView release];} - (void)dealloc { [superdealloc];}@end
CustomView.h
#import <UIKit/UIKit.h> @interfaceCustomView : UIView@property(nonatomic,copy)NSArray*passthroughViews;@end
CustomView.m
#import "CustomView.h" @interfaceCustomView()-(BOOL) isPassthroughView: (UIView*) view;@end @implementationCustomView{ BOOLtestHits; } @synthesizepassthroughViews=_passthroughViews; -(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent*)event{ if(testHits){ returnnil; } if(!self.passthroughViews || (self.passthroughViews && self.passthroughViews.count == 0)){ returnself; }else{ UIView*hitView = [superhitTest: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([selfisPassthroughView:superHitView]) { hitView = superHitView; } } returnhitView; }} - (BOOL)isPassthroughView:(UIView*)view { if(view == nil) { returnNO; } if([self.passthroughViews containsObject:view]) { returnYES; } return[selfisPassthroughView:view.superview];} -(void) dealloc{ self.passthroughViews = nil;} @end
- UIView 中的控件事件穿透 Passthrough 的实现
- UIView 中的控件事件穿透 Passthrough 的实现
- UIView 中的控件事件穿透 Passthrough 的实现
- UIView 中的控件事件穿透 Passthrough 的实现
- UIView 中的控件事件穿透 Passthrough 的实现
- UIView 中的控件事件穿透 Passthrough 的实现
- uiview点击事件穿透
- 关于控件的事件穿透
- iOS 使点击事件穿透透明的UIView
- 透明控件点击事件穿透的解决办法
- 控件点击事件会穿透给下面的界面--解决办法
- 控件点击事件会穿透给下面的界面--解决办法
- Unity中uGUI的控件事件穿透逻辑
- iOS 事件穿透,点击穿透,控件不响应事件
- VGA GPU passthrough 虚拟桌面pci穿透--Ubuntu与centos的内核升级步骤
- TimesTen中的passthrough模式
- 封装-给继承自UIView的控件添加点击事件
- 实现点击穿透、执行下层视图的点击事件
- 来广州传智播客学.Net 培训班给你真正的技术
- OpenGL教程
- 用户登录模块出现的问题
- Andoird ListView快速加载图片的终极解决之道
- iOS开发常用的第三方类库
- UIView 中的控件事件穿透 Passthrough 的实现
- 测试数据库连接是否正常 Sql server 2005
- Javascript_Email输入格式验证
- 变量管理
- 有CSDN博客了!
- flexpaper的使用的记录
- Javascript通过单选、复选、下拉列表获取数据
- Javascript_window对象
- JPush极光推送Java服务器端API