带有点击手势识别器的 UILabel 不起作用 [英] UILabel with Tap Gesture Recognizer not working

查看:30
本文介绍了带有点击手势识别器的 UILabel 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我设计 cell 时,在我的 viewcontroller 中,这是我的代码.

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 *按钮;UILabel *标签;if([发送者 isKindOfClass:[UIButton 类]]){按钮 = (UIButton *) 发送者;[self buyService:button.tag];}else if ([sender isKindOfClass:[UILabel class]]){标签 = (UILabel *) 发件人;[self buyService:label.tag];}}

但是添加的点击手势不起作用.

解决方案

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

所以,用一个按钮改变标签,并在它的 TouchUpInside 事件中设置 buyNowClicked: 方法.

更新:如果您因任何原因无法删除 UILabel,则在 UILabel 下放置一个 UIButton(与相同的 frame 并最终 NSConstraints) 并在按钮上的 TouchUpInside 事件中移动 buyNowClicked: 方法,然后您轻松获胜

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

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.

解决方案

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).

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

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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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