tpdietz
tpdietz

Reputation: 1368

UITapGestureRecognizer not working at all

I have been tasked to add an audio player into a pre-existing iPad/iPhone app. I have been struggling for the past two days trying to figure out why I cannot get a simple tap gesture to work. I have scoured the internet to no avail. I know there are several questions that already exist like this, but I am still very stuck. Please help! The area I am working in is inside a tableview with cells. I am adding a subiew into some of the cells which start from as a view called "background" and other views are added to it. I have added a simple UIView containing a play button. My "play button" view is constructed from a method like so:

-(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;
}

After creating this subview I append it to the cell view and try to add GestureRecognizers like so

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;

The corresponding tap function is

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

Inside ViewController.h, the class is declared as and I have added the following code inside the ViewController.m file

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

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

There is no .xib, this all done programatically.

Any ideas? Any thoughts on how I could begin to debug this?

EDIT:

I added the UITapGestureRecognizer to the UITableView and this will register a tap. Does anybody have any ideas how to get that tap to trickle down to the particular subview I want.

Upvotes: 0

Views: 283

Answers (2)

Geet
Geet

Reputation: 2437

It may be happening that the touch events of your table view would be cancelling the touch events of your UIView gesture, you may have to disable the table view selection of rows

Upvotes: 1

codester
codester

Reputation: 37189

You need to set userIntreactionEnable for UImageView try this

-(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;
}

Upvotes: 0

Related Questions