UILabel与Tap Gesture Recognizer无法正常工作

我已经使用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 *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]; } } 

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

使用tag属性创建custome cell并使用

 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%ld", (long)indexPath.row); } 

注意:在某个地方添加了TapGestureRecognizer,它可以防止选择单元格didselectitematindexpath

我的建议是重新设计您的设计并将UILabel更改为UIButton ,因为UIButton只是处理轻敲识别,而且您也没有任何问题也可以转发到UICollectionViewdidSelectItemAtIndexPath:确实会被调用)。

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

更新:如果你因为任何原因无法删除UILabel,那么在UILabel下放置一个UIButton (使用相同的frame ,最后是NSConstraints ),然后在按钮上的TouchUpInside事件中移动buyNowClicked:方法,然后轻松获胜

  1. 您必须将此代码添加到Your Custom UICollectionViewCell类中。

  2. 然后创建一个响应tap的委托。

  3. 在Collection View的cellForRowAtIndexPath中分配该委托。

如果您需要详细解释,请告诉我。

编辑1:代码:

MyCell.swift:

 import UIKit protocol MyCellDelegate { func lblNameTapped(cell: MyCell) } class MyCell: UICollectionViewCell { @IBOutlet var lblName: UILabel! var lblNameTapRec:UITapGestureRecognizer! var delegate: MyCellDelegate? override func awakeFromNib() { super.awakeFromNib() // Initialization code lblNameTapRec = UITapGestureRecognizer(target: self, action: #selector(MyCell.lblNameTapped(_:))) lblName.userInteractionEnabled = true lblName.addGestureRecognizer(lblNameTapRec) } func lblNameTapped(sender: AnyObject){ delegate?.lblNameTapped(self) } } 

ViewController.Swift:

 import UIKit class WallOfPraizeVC: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,MyCellDelegate { @IBOutlet var collectionView: UICollectionView! //DelegateMethod func lblNameTapped(cell: MyCell) { let indexPath = self.collectionView.indexPathForCell(cell) print(indexPath!.row) } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! MyCell cell.delegate = self return cell } } 

尝试这个 :-

 -(void)buyNowClicked:(UITapGestureRecognizer*)tap { NSLog(@"buyNowClicked here"); [self buyService:tap.view.tag]; } 

使用此代码轻击手势: –

  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]; Try this : -(void)buyNowClicked : :(UITapGestureRecognizer*)recognizer { UIView *view = [recognizer view]; NSLog(@"tag %ld",(long)view.tag); UIButton *button; UILabel *label; if([view isKindOfClass:[UIButton class]]) { button = (UIButton *)sender; [self buyService:button.tag]; } else if ([view isKindOfClass:[UILabel class]]) { label = (UILabel *)sender; [self buyService:label.tag]; } }