2017-07-25 98 views
0

添加长按手势在UICollectionViewCell一个标签,我尝试添加长按手势的UICollectionView细胞标记,但它不工作。我怎样才能在斯威夫特

这里是我尝试:

private lazy var longpressGesture: UILongPressGestureRecognizer = { 
     let gesture = UILongPressGestureRecognizer(target: self, action: #selector(longpressGestureRecognizer)) 
     return gesture 
    }() 


@objc private func longpressGestureRecognizer(gestureRecognizer: UIGestureRecognizer) { 
    print("here") 
} 

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell { 
     cell.label.addGestureRecognizer(longpressGesture) 
    } 
} 

而且CustomCell:当我按下标签

class CalendarSlotCell: UICollectionViewCell { 
    let label: UILabel = { 
     let lb = UILabel() 
     lb.text = "Pressme" 
     return lb 
    }() 
} 

什么也没有发生。

注意:我可以补充长按手势细胞像下面,它的工作,但我想要做的就是添加长按手势标签细胞没有只是细胞

添加长按手势细胞,在viewDidLoad中功能:

override func viewDidLoad() { 
     super.viewDidLoad() 
     collectionView?.addGestureRecognizer(panGesture) 
} 

编辑

它不喜欢this post,因为当我更换的UILabel的UIImageView或其他任何这是行不通的太。

另外,如果我的问题是的UILabel与双击手势识别器不工作我可以解决这个问题是这样的:

var tap = UITapGestureRecognizer(target: self, action: #selector(handleTap)) 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) as! CustomCell 
     cell.label.addGestureRecognizer(tap) 
     return cell 
    } 

感谢您的帮助!

+0

可能的复制(https://stackoverflow.com/questions/39163577/uilabel-with-tap -resture-recognizer-not-working) –

+0

很明显,当你第一次点击时没有发生任何事情,因为你在didSelect方法中添加了手势识别器。而是在cellForRow方法中添加手势 –

回答