AVEbrahimi
AVEbrahimi

Reputation: 19134

Adding touch to UIImageView is not working

I tried to add tap to UIImageView, as below, but it's not working! _img_compass_text_background is UIImageView

I found another view is covering my view, but most of top view is full transparent, how can I force taps pass through it?

In other words how can I disable a view from consuming taps?

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
_img_compass_text_background.userInteractionEnabled=YES;
[_img_compass_text_background addGestureRecognizer:tapGestureRecognizer];

Upvotes: 1

Views: 223

Answers (5)

user2031400
user2031400

Reputation: 41

Your code is right how ever there must be some problem of adding UIImageView to its parent view or another view is covering it try this code if another view is over it

[parentView bringSubviewToFront:childView];

or check is this added to its parents view properly or not

Upvotes: 0

yasirmturk
yasirmturk

Reputation: 1954

might be some other view is over your UIImageView ..in the case just disable user interaction for the upper view so the under lying view will get the touch events

Upvotes: 0

Schrodingrrr
Schrodingrrr

Reputation: 4271

You say your imageView has another view on top ?

In that case, there are many things you could do:

  • Add tapGesture on the overlapping view, and in the target method, check if the tapped point is contained in the rect of the imageView frame. Use locationInView and CGRectContainsPoint.

  • Use touchesBegan/Moved/Ended and check if the imageView frame contains the point of touch using containsPoint.

EDIT:

Add tap gesture to your overlapping view, with target method something like this:

- (void) targetMethod:(UITapGestureRecogniser*)gesture{

    CGPoint tapPoint = [gesture locationInView:(the view that contains both the views)];

    if (CGRectContainsPoint(imageView.frame, tapPoint)){

        //your code

    }

}

Upvotes: 1

Vinodh
Vinodh

Reputation: 5268

If you are having too many images set tag for them and addding

- (void)viewDidLoad
    {
        [super viewDidLoad];


        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        _background.userInteractionEnabled=YES;
         _background.tag = 0;
        [_background addGestureRecognizer:tapGestureRecognizer];
    }

    - (void)locationNameTapped:(UITapGestureRecognizer*)gesture
    {
          if(gesture.view.tag == 0)
        {
                      NSLog(@"image tapped");

        }
    }

Upvotes: 0

Jordan Montel
Jordan Montel

Reputation: 8247

Use an IBOutlet to link your image :

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

and named your property without underscore _ and use CamelCase for your property.

This code works for me :

- (void)viewDidLoad
{
    [super viewDidLoad];


    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationNameTapped:)];
    tapGestureRecognizer.numberOfTapsRequired = 1;
    _background.userInteractionEnabled=YES;
    [_background addGestureRecognizer:tapGestureRecognizer];
}

- (void)locationNameTapped:(UITapGestureRecognizer *)gesture
{
    NSLog(@"image tapped");
}

Upvotes: 0

Related Questions