Objective c 防止在UITableVIew子视图中执行父视图UIGestureRecongnizer
问题: 我有一个作为子视图的。UIView具有已配置的 我的问题是,桌面上的点击被UIView的手势识别器消耗掉了。结果是表视图永远看不到抽头 当某个点位于表格的框架内时,如何使识别器失败,或者如何使表格成为tap的默认使用者 我尝试过(如代码示例所示)许多方法,但不太清楚如何做到这一点 下面是表示问题的代码: 控制器:Objective c 防止在UITableVIew子视图中执行父视图UIGestureRecongnizer,objective-c,ios,uitableview,uigesturerecognizer,Objective C,Ios,Uitableview,Uigesturerecognizer,问题: 我有一个作为子视图的。UIView具有已配置的 我的问题是,桌面上的点击被UIView的手势识别器消耗掉了。结果是表视图永远看不到抽头 当某个点位于表格的框架内时,如何使识别器失败,或者如何使表格成为tap的默认使用者 我尝试过(如代码示例所示)许多方法,但不太清楚如何做到这一点 下面是表示问题的代码: 控制器: #import <UIKit/UIKit.h> #import "ABCDTableView.h" @interface ABCDFirstView : UIVi
#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
#导入
#导入“ABCDTableView.h”
@界面ABCDFirstView:UIView
@属性(非原子,强)ABCDTableView*tableView;
@结束
#导入“ABCDFirstView.h”
@第一视图的实现
@综合桌面视图;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
[从NIB自我唤醒];
}
回归自我;
}
-(无效)从NIB中唤醒
{
UITapGestureRecognizer*点击=[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(viewTouch:)];
[自选地址识别器:点击];
}
-(无效)viewTouch:(UIgestureRecognitor*)手势{
NSLog(@“触摸视图”);
}
-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event
{
if(CGRectContainsPoint(self.tableView.bounds,point)){
NSLog(“表中点和视图”);
返回否;
}
else if(CGRectContainsPoint(self.bounds,point)){
NSLog(“仅在视图中的点”);
返回YES;
}
NSLog(@“不在视图中的点”);
返回否;
}
-(UIView*)hitTest:(CGPoint)point with event:(UIEvent*)event
{
UIView*view=[super-hitTest:pointwithevent:event];
NSLog(@“查看命中率:%@”,查看);
返回视图;
}
@结束
桌面视图
#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
#导入
@界面ABCDTableView:UITableView
@属性(非原子,强)NSArray*列表;
@结束
#导入“ABCDTableView.h”
@实现ABCDTableView
@综合列表;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
[从NIB自我唤醒];
}
回归自我;
}
-(无效)从NIB中唤醒
{
self.delegate=self;
self.dataSource=self;
//创建表列表
self.list=[[NSArray alloc]
initWithObjects:@“一”,“二”,“三”,“四”,“五”,
@"六,"七,"八,"九,"十,零";;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.list count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellID=@“cell”;
UITableViewCell*cell=[自出队列可重用CellWithIdentifier:cellID];
如果(单元格==nil){
单元格=[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault重用标识符:cellID];
}
cell.textlab.text=[self.list objectAtIndex:[indexPath行]];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
NSLog(@“选定行”);
}
@结束
看法
#导入
#导入“ABCDTableView.h”
@界面ABCDFirstView:UIView
@属性(非原子,强)ABCDTableView*tableView;
@结束
#导入“ABCDFirstView.h”
@第一视图的实现
@综合桌面视图;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
[从NIB自我唤醒];
}
回归自我;
}
-(无效)从NIB中唤醒
{
UITapGestureRecognizer*点击=[[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(viewTouch:)];
[自选地址识别器:点击];
}
-(无效)viewTouch:(UIgestureRecognitor*)手势{
NSLog(@“触摸视图”);
}
-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event
{
if(CGRectContainsPoint(self.tableView.bounds,point)){
NSLog(“表中点和视图”);
返回否;
}
else if(CGRectContainsPoint(self.bounds,point)){
NSLog(“仅在视图中的点”);
返回YES;
}
NSLog(@“不在视图中的点”);
返回否;
}
-(UIView*)hitTest:(CGPoint)point with event:(UIEvent*)event
{
UIView*view=[super-hitTest:pointwithevent:event];
NSLog(@“查看命中率:%@”,查看);
返回视图;
}
@结束
使用UIgestureRecognitizerDelegate方法
-(BOOL)手势识别器:(UIGestureRecognitor*)手势识别器应接受触摸:(UITouch*)触摸
并检查touch.view是否与应接收手势识别器的视图相同
#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