简体   繁体   中英

UILabel with Tap Gesture Recognizer not working

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

create custome cell with tag property and use

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

Note: added TapGestureRecognizer somewhere and it prevents selecton of cell didselectitematindexpath

  1. You have to add this code into the Your Custom UICollectionViewCell class.

  2. Then create a delegate which will respond to the tap.

  3. Assign that delegate in cellForRowAtIndexPath of Collection View.

let me know if you need detailed explanation.

Edit 1: Code:

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

Try this :-

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

 }

Use this code for tap gesture: -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM