UITapGestureRecognizer根本不工作

时间:2014-07-22 19:12:40

标签: ios objective-c uitableview

我的任务是将音频播放器添加到预先存在的iPad / iPhone应用程序中。在过去的两天里,我一直在努力想弄清楚为什么我不能用一个简单的敲击手势来工作。我在互联网上搜索无济于事。我知道有几个问题已经存在,但我仍然非常困难。请帮忙!我正在工作的区域是一个带有单元格的tableview。我在一些单元格中添加一个子视图,这些单元格从一个名为" background"并添加了其他视图。我添加了一个包含播放按钮的简单UIView。我的播放按钮"视图是通过如下方法构建的:

-(UIView*) messagePlayerImageViewForMessage:(LAZMessage*) message {
UIView* returnView = [[UIView alloc] initWithFrame:CGRectZero];
UIImageView* playButtonImageView = [[UIImageView alloc] init];
[returnView addSubview:playButtonImageView];
playButtonImageView.tag = TAG_CELL_PLAY_IMAGE_VIEW;

if ([self stringForMessageRecordingId:message]) {
    UIImage* playButton = [UIImage imageNamed:@"play"];
    [playButtonImageView setImage:playButton];
    [playButtonImageView setFrame:CGRectMake(0, 0, playButton.size.width/2, playButton.size.height/2)];

    [returnView setFrame:CGRectMake(0, 0, playButtonImageView.frame.size.width, playButtonImageView.frame.size.height)];
}
returnView.tag = TAG_CELL_MESSAGE_PLAYER_VIEW;
return returnView;
}

创建此子视图后,我将其附加到单元格视图并尝试添加GestureRecognizers,如此

UIView* messagePlayerView = [self messagePlayerImageViewForMessage:message];
messagePlayerView.tag = TAG_CELL_MESSAGE_PLAYER_VIEW;
messagePlayerView.userInteractionEnabled = YES;
[background addSubview:messagePlayerView];

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[messagePlayerView addGestureRecognizer:tapGesture];
tapGesture.cancelsTouchesInView = NO;
tapGesture.delegate = self;

相应的点按功能

-(void) handleTap:(UITapGestureRecognizer*) tapGestureRecognizer{
    NSLog(@"hi");
}

在ViewController.h中,该类被声明为,我在ViewController.m文件中添加了以下代码

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

没有.xib,这一切都是以编程方式完成的。

有什么想法吗?关于我如何开始调试它的任何想法?

编辑:

我将UITapGestureRecognizer添加到UITableView,这将注册一个点按。有没有人有任何想法如何让我们点击我想要的特定子视图。

2 个答案:

答案 0 :(得分:1)

可能正在发生的是,您的表格视图的触摸事件将取消您的UIView手势的触摸事件,您可能必须禁用行视图选择行

答案 1 :(得分:0)

您需要为userIntreactionEnable设置UImageView试试此

-(UIView*) messagePlayerImageViewForMessage:(LAZMessage*) message {
UIView* returnView = [[UIView alloc] initWithFrame:CGRectZero];
UIImageView* playButtonImageView = [[UIImageView alloc] init];
[returnView addSubview:playButtonImageView];
playButtonImageView.userInteractionEnabled = YES;
playButtonImageView.tag = TAG_CELL_PLAY_IMAGE_VIEW;

if ([self stringForMessageRecordingId:message]) {
    UIImage* playButton = [UIImage imageNamed:@"play"];
    [playButtonImageView setImage:playButton];
    [playButtonImageView setFrame:CGRectMake(0, 0, playButton.size.width/2, playButton.size.height/2)];

    [returnView setFrame:CGRectMake(0, 0, playButtonImageView.frame.size.width, playButtonImageView.frame.size.height)];
}
returnView.tag = TAG_CELL_MESSAGE_PLAYER_VIEW;
return returnView;
}