UITapRecognizer not working with ImageView

101 views Asked by At

I am currently trying to make a signout button for an app.

I have the UIImageView "User Interaction Enabled" box checked.

in my .h file I have this:

@property (strong, nonatomic) IBOutlet UIImageView *signOutButtonIV;

in my .m file I have this:

- (void) viewDidLoad{

[super viewDidLoad];

    UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResponder:)];

    tapGestureRecognizer.numberOfTapsRequired = 1;
    tapGestureRecognizer.numberOfTouchesRequired = 1;
    [self.signOutButtonIV addGestureRecognizer:tapGestureRecognizer];

}

-(void) tapResponder:(UITapGestureRecognizer *) sender{
    NSLog(@"single tap detected");
}

If I instead use [self addGestureRecognizer:tapGestureRecognizer] the taps will be detected. Is there a step I am missing?

3

There are 3 answers

1
Kuldeep Tanwar On

Can also use storyboard to achieve the results:-

enter image description here

0
Pushkraj Lanjekar On

Add following lines

[self.signOutButtonIV setUserInteractionEnabled:YES]; 
[PARENT_VIEW bringSubviewToFront:self.signOutButtonIV];

before [self.signOutButtonIV addGestureRecognizer:tapGestureRecognizer];

0
Airagale On

Thanks to Artem Novichkov pointed out to Log the userEnabledInteraction.

The log came back false. Even though i had checked the box in the storyboard file it was not enabled.

[signOutButtonIV setUserInteractionEnabled:YES];

was what was needed to fix my problem.

Thanks Artem!