UILabel与Tap Gesture Recognizer无法正常工作

时间:2016-08-26 09:59:05

标签: ios objective-c uicollectionview uicollectionviewcell uitapgesturerecognizer

我使用var root = System.Web.Hosting.HostingEnvironment.MapPath("~/bin"); 设计了一个UICollectionViewCell,在该自定义单元格中,我有XIB我已启用的用户互动。

在我设计UILabel时的viewcontroller中,这是我的代码。

cell

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

5 个答案:

答案 0 :(得分:1)

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

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

更新:如果因任何原因无法移除UILabel,请在UIButton下放置UILabel(同一frame,最后{ {1}})并在按钮上的NSConstraints事件处移动buyNowClicked:方法,然后轻松获胜

答案 1 :(得分:1)

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

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

注意:在某处添加了TapGestureRecognizer,它会阻止选择单元格didselectitematindexpath

答案 2 :(得分:0)

  1. 您必须将此代码添加到您的自定义UICollectionViewCell课程中。

  2. 然后创建一个响应该点按的委托。

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

  4. 如果您需要详细解释,请与我们联系。

    编辑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
        }
    }
    

答案 3 :(得分:0)

试试这个: -

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

 }

答案 4 :(得分:0)

使用此代码进行点击手势: -

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