开发者

iOS - ignoring a tap gesture

开发者 https://www.devze.com 2023-03-30 14:22 出处:网络
I have a UITapGestureRecognizer in a custom table cell which is intended to do nothing (remove the ability to tap on a c开发者_运维百科ell to select it). This works great however there are some button

I have a UITapGestureRecognizer in a custom table cell which is intended to do nothing (remove the ability to tap on a c开发者_运维百科ell to select it). This works great however there are some buttons (subviews) in the cell which cannot be tapped because the tap gesture handles that whole cell area.

So its as simple as detecting when the touch gesture is over one of those buttons and returning false to cancel that particular gesture, right? Well not for me ...

I have removed the logic and simply returned NO in the gesture recogniser, but I still can't tap the buttons.

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

Is there something I am missing here?


The solution is more simple: you shouldn't be using a gesture recognizer to do this. If you don't want a cell to be selectable, you can do two things:

  • return nil from -tableView:willSelectRowAtIndexPath: to make it unselectable
  • set the cell's selectionStyle to UITableViewCellSelectionStyleNone to remove the highlight effect

Doing it this way should preserve your button functionality.


Edit: if you don't want to do that, then you can do what you were originally trying — except I think you have the method name wrong, it should be this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch


to do this, I made a custom button like this:

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
    [self addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self addTarget:self action:@selector(touchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
}

- (void)touchDown
{
    self.imageView.image = [UIImage imageNamed:@"ButtonPressed.png"];
}

- (void)touchUpInside
{
    self.imageView.image = [UIImage imageNamed:@"Button.png"];
}

- (void)touchUpOutside
{
    self.imageView.image = [UIImage imageNamed:@"Button.png"];
}

- (void)setHighlighted:(BOOL)highlighted
{
    return;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号