咱们在有多个 UIView 层叠时,好比一个按钮被一个 UIView 遮盖时,想要在点击最上层的 UIView 时能触发按钮的相应事件,咱们该如何实现呢,初步能够想到几种办法:html
1. 把按钮上层的全部 UIView 的 userInteractionEnabled 属性设置为 NO,要是 UIView 有本身的交互事件该如何办呢?并且这个 userInteractionEnabled 不能动态设置,等到点击后决定设置它的 NO 是没用的git
2. UIView 接受到点击事件后主动去触发下面按钮的点击,这时的关题有三,按钮没有点击过程当中的交换效果、多层 UIView 时不切实际,逐层下传吗、还有就是其余双击、三击或别的手势如何处理github
我也一直被前面两种方式纠缠着,同时也让 UIPopoverController 的 NSArray *passthroughViews 属性提醒着,由于对于 UIPopoverController,设置到它的 passthoughViews 属性中的控件事件能够彻底从 UIDimmingView 下透出来。但苦于不可能看到 UIPopoverController 的源码,仍是后面一而再的 Google 终于发现了 UIView 的方法:ide
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; // recursively calls -pointInside:withEvent:. point is in the receiver's coordinate system
只要实现了最顶层的 UIView 的 hitTest 方法,在某些状况返回下层的某个按钮实例,即至关于把那个按钮的事件透出来了,好比在点击落在该按钮上时,无论这个按钮在 UIView 下多少层均可以把它挖出来。this
关键要理解 hitTest 方法的做用,可参考:atom
1.http://blog.sina.com.cn/s/blog_677089db01012wpg.html
2. https://github.com/werner77/WEPopoverspa
有两部分代码,分别是 ViewController 和 CustomController.net
// // 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.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 // 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 // // 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