我有一个图像,当用户在该图像上点击两次时,我会显示一个带有勾号的按钮,就好像用户已经勾选了该图像一样。我已将按钮设置为首先从 Storyboard 中隐藏。
我正在使用这个进行手机窃听
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.subOptionsCollectionView{
let imageNamed = "\(customizeOptionSelected[indexPath.row])"
shirtImage.image = UIImage(named: imageNamed)
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
collectionView.addGestureRecognizer(tap)
}
}
func doubleTapped() {
print("Double Tap")
}
但是我该如何显示那个勾号/按钮呢?
最佳答案
将您的代码放在 cellForRowAtIndexPath
而不是 didSelect
并禁用 collectionView 的 userInteraction
然后您可以将按钮的 isHidden 属性设置为 true在 doubleTapped
中,但您必须像这样更改函数 (Swift3):
func doubleTapped(selectedIndex: IndexPath) {
print("Double Tap")
}
并像这样更改选择器:
UITapGestureRecognizer(target: self, action: self.doubleTapped(selectedIndex: indexPath))
还有一个解决方案:
将您的代码放在 cellForRowAtIndexPath
而不是 didSelect
中,然后您可以在 doubleTapped
中将按钮的 isHidden 属性设置为 true,但是您有像这样改变函数(Swift2):
func doubleTapped(sender: AnyObject) {
let buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.collectionView)
let indexPath: NSIndexPath = self.collectionView.indexPathForRowAtPoint(buttonPosition)!
//you have the selected cell index
let cell = self.collectionView.cellForItemAtIndexPath(indexPath)
//now you have the cell and have access to the button
}
然后像这样添加手势:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.doubleTapped(_:)))
cell.addGestureRecognizer(tapGesture)
关于ios - UICollectionView Cell Tapped 时显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41257009/