UILabel与Tap Gesture Recognizer无法正常工作 [英] UILabel with Tap Gesture Recognizer not working

查看:575
本文介绍了UILabel与Tap Gesture Recognizer无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XIB 设计了 UICollectionViewCell ,在该自定义单元格中我有一个 UILabel 我已启用其用户互动。

I have designed a UICollectionViewCell using XIB and in that custom cell I have an UILabel whose user interaction I have enabled.

我设计时的 viewcontroller 单元格,这是我的代码。

In my viewcontroller when I am designing the cell, Here is my code.

UITapGestureRecognizer *buyNowTapped = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buyNowClicked:)];
buyNowTapped.numberOfTapsRequired = 1.0;
cell.buy.tag = indexPath.row;
cell.buy.userInteractionEnabled = YES;
[cell.buy addGestureRecognizer:buyNowTapped];

-(void)buyNowClicked : (id)sender
{
  UIButton *button;
  UILabel *label;
  if([sender isKindOfClass:[UIButton class]])
  {
      button = (UIButton *)sender;
      [self buyService:button.tag];
  }
  else if ([sender isKindOfClass:[UILabel class]])
  {
    label = (UILabel *)sender;
    [self buyService:label.tag];
  }
}

但添加的点按手势不起作用。

But the added tap gesture does not work.

推荐答案

我的建议是重新设计并将 UILabel 更改为 UIButton ,因为 UIButton 只是处理点击识别,而且你也没有任何转发问题也转发到 UICollectionView (确实会调用 didSelectItemAtIndexPath:)。

My suggestion is to rework your design and change the UILabel to an UIButton, because UIButton simply handles tap recognition, and also you do not have any issue of tap forwarded also to the UICollectionView (didSelectItemAtIndexPath: will be called indeed).

所以,使用按钮更改标签,并在 TouchUpInside 事件上设置 buyNowClicked:方法。

So, change the label with a button and set buyNowClicked: method at TouchUpInside event on it.

更新:如果您因任何原因无法移除UILabel,请在<下方放置 UIButton code> UILabel (使用相同的 frame 并最终 NSConstraints )并移动 buyNowClicked:方法按钮上的TouchUpInside 事件然后你轻松获胜

UPDATE: If you can't remove the UILabel for any reason, then put a UIButton under the UILabel (with same frame and eventually NSConstraints) and move buyNowClicked: method at TouchUpInside event on the button and then you win easily

这篇关于UILabel与Tap Gesture Recognizer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆