UICollectionView Cell Tapped 时显示按钮
Display button when UICollectionView Cell Tapped
我有一张图片,当用户在该图片上点击两次时,我会显示一个带有勾号的按钮,就好像用户已经勾选了该图片一样。我已将按钮设置为首先从情节提要中隐藏。
我正在使用这个进行手机窃听
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")
}
但是如何显示 tick/button?
将你的代码放在cellForRowAtIndexPath
而不是didSelect
中并禁用collectionView的userInteraction
然后你可以在[=中将按钮的isHidden 属性设置为true 17=],但你必须像这样改变函数(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)
Swift 4更新:
@IBAction func doubleTap(_ sender: UITapGestureRecognizer) {
let buttonPosition: CGPoint = sender.location(in: self.collectionView)
guard let indexPath = self.collectionView?.indexPathForItem(at: buttonPosition) else { return }
print ("doubleTap on cell at: ", indexPath)
let cell = self.collectionView.cellForItem(at: indexPath)
// now you have the cell and have access to the button
}
我有一张图片,当用户在该图片上点击两次时,我会显示一个带有勾号的按钮,就好像用户已经勾选了该图片一样。我已将按钮设置为首先从情节提要中隐藏。
我正在使用这个进行手机窃听
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")
}
但是如何显示 tick/button?
将你的代码放在cellForRowAtIndexPath
而不是didSelect
中并禁用collectionView的userInteraction
然后你可以在[=中将按钮的isHidden 属性设置为true 17=],但你必须像这样改变函数(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)
Swift 4更新:
@IBAction func doubleTap(_ sender: UITapGestureRecognizer) {
let buttonPosition: CGPoint = sender.location(in: self.collectionView)
guard let indexPath = self.collectionView?.indexPathForItem(at: buttonPosition) else { return }
print ("doubleTap on cell at: ", indexPath)
let cell = self.collectionView.cellForItem(at: indexPath)
// now you have the cell and have access to the button
}