问题:
我有一个UIView,它有一个UITableView作为子视图。UIView 配置了一个UITapGestureRecognizer。
我的问题是 UIView 的手势识别器消耗了桌子上的点击。结果是表格视图永远不会看到水龙头。
当一个点在表格的框架内时,如何使识别器失败,或者使表格成为水龙头的默认使用者。
我已经尝试过(如代码示例所示)一些方法pointInside:withEvent,hitTest:withEvent但不能完全弄清楚如何去做。
这是代表问题的代码:控制器:
#import <UIKit/UIKit.h>
#import "ABCDTableView.h"
@interface ABCDFirstView : UIView
@property (nonatomic,strong) ABCDTableView *tableView;
@end
#import "ABCDFirstView.h"
@implementation ABCDFirstView
@synthesize tableView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(viewTouch:)];
[self addGestureRecognizer:tap];
}
- (void)viewTouch:(UIGestureRecognizer *)gesture {
NSLog(@"view touched");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSLog(@"point in table point as well as view");
return NO;
}
else if (CGRectContainsPoint(self.bounds, point)) {
NSLog(@"point only in view");
return YES;
}
NSLog(@"point not in view");
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
NSLog(@"view hittest res: %@",view);
return view;
}
@end
表视图
#import <UIKit/UIKit.h>
@interface ABCDTableView : UITableView
<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSArray *list;
@end
#import "ABCDTableView.h"
@implementation ABCDTableView
@synthesize list;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
self.delegate = self;
self.dataSource = self;
// create table list
self.list = [[NSArray alloc]
initWithObjects: @"one",@"two",@"three",@"four",@"five",
@"six", @"seven", @"eight", @"nine", @"ten", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cell";
UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [self.list objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"row selected");
}
@end
看法
#import <UIKit/UIKit.h>
#import "ABCDTableView.h"
@interface ABCDFirstView : UIView
@property (nonatomic,strong) ABCDTableView *tableView;
@end
#import "ABCDFirstView.h"
@implementation ABCDFirstView
@synthesize tableView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(viewTouch:)];
[self addGestureRecognizer:tap];
}
- (void)viewTouch:(UIGestureRecognizer *)gesture {
NSLog(@"view touched");
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (CGRectContainsPoint(self.tableView.bounds, point)) {
NSLog(@"point in table point as well as view");
return NO;
}
else if (CGRectContainsPoint(self.bounds, point)) {
NSLog(@"point only in view");
return YES;
}
NSLog(@"point not in view");
return NO;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
NSLog(@"view hittest res: %@",view);
return view;
}
@end