Domanda

I am running into a problem I hope someone can help me with.

I have a view that has a pan gesture which works fine and I can pan around an image that I have displayed. It is the only gesture that is added.

I then add a UIView (a custom view called headerView) with buttons on it that I add programmatically..

  • It displays the buttons correctly
  • I have set its delegate to self
  • I have confirmed that the button's selectors are being set
  • I have used headerView before on a different form and the buttons work fine.
  • I have brought my headerView to the the "front"

The problem I am encountering is that on this form with the pan gesture, the buttons on my headerView don't respond to my taps. If I pan directly on my headerView, I am able to to move around the underlying image so the pan gesture seems to be over riding the tap gestures on my headerView preventing any of my header buttons from working.

How do I fix it so the pan gesture doesn't prevent tapping in my headerView?

Thanks!

SOMEWHAT SOLVED... To update this post. I was able to get a response by manually setting the headerView with a tap gesture...

self.headerView = [[OAPhotoHeaderView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, self.view.window.bounds.size.width, 100.0f) buttons:OAPhotoHeaderButtonsComment| OAPhotoHeaderButtonsLike |OAPhotoHeaderButtonsFlag];//was 44.0f
self.headerView.delegate = self;
[self.headerView setUserInteractionEnabled:YES];
self.headerView.backgroundColor = [UIColor grayColor];
self.headerView.photo=user;
[self.headerView setPhoto:self.userPhotoObject];//  self.myUserPhotoObject];
[self.headerView.likeButton setUserInteractionEnabled:YES];
//[self.headerView.likeButton addTarget:self action:@selector(didTapLikePhotoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.headerView setUserInteractionEnabled:YES];
[self.headerView setOpaque:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self.headerView action:@selector(didTapLikePhotoButtonAction:)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[self.headerView addGestureRecognizer:tap];
[self setExclusiveTouchForButtons:self.headerView];

This actually fires the button handler in the headerView class as if someone pressed the button. However, this won't work as they can press anywhere on the headerView and it will fire. I can't seem to find the proper way to assign the tap gesture to the button

[self.headerView.likeButton addGestureRecognizer:tap]

doesn't work. Also, this doesn't work either...

[self.headerView.likeButton addTarget:self action:@selector(didTapLikePhotoButtonAction:) forControlEvents:UIControlEventTouchUpInside];

Nessuna soluzione corretta

Altri suggerimenti

You should add another gesture recognise methods which allow you to cancel it if you tap on your button:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
     // Replace to your view with button
    if ((self.headerView isDescendantOfView:touch.view))
    {
        return NO;
    }
    return YES;
}

Remember to add your view controller as a delegate of gesture recogniser. Hope this help.

Every time I can't get touch working on something of mine it turns out that the object is visible but not actually within the bounds of the containing view -- because the view doesn't have clipToBounds set. Perhaps your containing view bounds aren't what you think they are. In any case, another avenue to pursue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top