1
votes

I have added UITapGesture on UIImageView. Have added all the code. ImageView description is showing the gestures, but still it is not working.

@interface VideoBaseProgramEditorViewController ()<UIGestureRecognizerDelegate> {

UITapGestureRecognizer* tapGesture;
}

...

 tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
              [tapGesture setNumberOfTapsRequired:1];
              tapGesture.delegate = self;
              thumbImageView.userInteractionEnabled = YES;
             [thumbImageView addGestureRecognizer:tapGesture];

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    UIView *view = touch.view;
    NSLog(@"%@",view);
    return YES;
}

-(void) viewTapped {

  [self loadVideo];
}

Issue: shouldReceiveTouch method is also not calling.

Image view description is showing added gesture in logs.

UIImageView: 0x7bb7b370; frame = (0 0; 736 485); opaque = NO; autoresize = W+H; gestureRecognizers = NSArray: 0x7bc80910; layer = CALayer: 0x78f793d0

any help appreciated.

3
Could you also share viewTapped method? - EDUsta
sure. Plz check edits. - Krutika Sonawala
Your code is working. - Sunny
it's not working in my page :( as I said even shouldReceiveTouch method not calling. - Krutika Sonawala
How you are adding imageView in your ViewController ? through storyboard ? or manually by code ? - CodeChanger

3 Answers

0
votes

You need to enable user interaction on UIImageView which is disabled by default. On your storyboard or xib select your UIImageView and in Attribute Inspector tick the User Interaction Enabled.

0
votes

try this.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [super touchesBegan:touches withEvent:event];
        NSLog(@"Successfully Tap");
    }
0
votes

Do you link your IBOutlet thumbImageView ? How do you declare your properties ?

Maybe try this following code. Work with userInteractionEnabled both code and xib.

#import "ViewController.h"

@interface ViewController () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UITapGestureRecognizer* tapGesture;

@property (nonatomic, weak) IBOutlet UIImageView *thumbImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    _tapGesture.delegate = self;
    [_tapGesture setNumberOfTapsRequired:1];
    // _thumbImageView.userInteractionEnabled = YES; // Uncomment if you allow interaction from code
    [_thumbImageView addGestureRecognizer:_tapGesture];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    UIView *view = touch.view;
    NSLog(@"%@",view);
    return YES;
}

- (void)viewTapped:(UITapGestureRecognizer *)sender {
    UIView *view = sender.view;
    NSLog(@"%@",view);
}

@end