iOS - 忽略点击手势

发布于 2024-12-01 03:48:34 字数 385 浏览 1 评论 0原文

我在自定义表格单元格中有一个 UITapGestureRecognizer,它不执行任何操作(删除点击单元格来选择它的功能)。这非常有效,但是单元格中有一些按钮(子视图)无法点击,因为点击手势处理整个单元格区域。

因此,它就像检测触摸手势何时位于这些按钮之一上并返回 false 以取消该特定手势一样简单,对吗?好吧,不适合我......

我已经删除了逻辑,只是在手势识别器中返回“否”,但我仍然无法点击按钮。

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

我在这里缺少什么吗?

I have a UITapGestureRecognizer in a custom table cell which is intended to do nothing (remove the ability to tap on a cell 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总以为 2024-12-08 03:48:34

解决方案更简单:您不应该使用手势识别器来执行此操作。如果您不希望单元格可选,可以执行以下两件事:

这样做应该保留您的按钮功能。


编辑:如果你不想这样做,那么你可以做你最初尝试的事情 - 除非我认为你的方法名称错误,它应该是这样的:

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

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:

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
梦里兽 2024-12-08 03:48:34

为此,我制作了一个如下所示的自定义按钮:

- (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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文