ios - UICollectionView Cell Tapped 时显示按钮

标签 ios swift uicollectionview uicollectionviewcell

我有一个图像,当用户在该图像上点击两次时,我会显示一个带有勾号的按钮,就好像用户已经勾选了该图像一样。我已将按钮设置为首先从 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/

相关文章:

ios - UITableViewCell 在运行时看起来与 InterfaceBuilder 中不同(在 iPad 上)

ios - 如何修复错误 "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeca55ff8)"

ios - UICollectionViewFlowLayout 子类 Sticky 和 ​​Stretchy header

ios - 如何在 ios 应用程序中的谷歌地图上添加多个引脚

ios - 无法在 Swift 中观察 UIPageControl 的 currentPage 变化

iphone - AVplayer 2-3 次后未在 ScrollView 中显示

ios - 如何将标签设置为UICollectionView-SWIFT-同一屏幕上的多个CollectionView

IOS Swift 图像加载到 UIImageView 时旋转

ios - 将单个 swift 文件添加到现有的 objective-c 项目

swift - 如何将当前日期保存到 CoreData?