问题:
我有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
的TableView
#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
答案 0 :(得分:3)
使用UIGestureRecognizerDelegate方法
- (BOOL)gestureRecognizer :( UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch :( UITouch *)touch;
并检查touch.view是否与应该接收手势识别器的视图相同。