collectionView

时间:2018-07-10 12:26:01

标签: ios swift

我有代码:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return productsObjectArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell10", for: indexPath) as! MainViewCollectionViewCell
        cell.titleLabel.text = productsObjectArray[indexPath.item].name
        let documentsDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let imageFileName = productsObjectArray[indexPath.item].code
        let fullImagePath = documentsDir.appendingPathComponent("GET_PHOTO").path + "/" + imageFileName! + ".jpg"
        cell.imageView.image = UIImage(contentsOfFile:   fullImagePath)

        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.rightSwiped))
        swipeRight.direction = UISwipeGestureRecognizerDirection.right
        cell.addGestureRecognizer(swipeRight)

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MainViewControler.leftSwiped))
        swipeLeft.direction = UISwipeGestureRecognizerDirection.left
        cell.addGestureRecognizer(swipeLeft)

        cell.favoriteBtn1.tag =  indexPath.item
        cell.favoriteBtn1.addTarget(self, action: #selector(favoriteBtnPressed1(_:)), for: .touchUpInside)

        cell.favoriteBtn2.tag =  indexPath.item
        cell.favoriteBtn2.addTarget(self, action: #selector(favoriteBtnPressed2(_:)), for: .touchUpInside)

        cell.favoriteBtn3.tag =  indexPath.item
        cell.favoriteBtn3.addTarget(self, action: #selector(favoriteBtnPressed3(_:)), for: .touchUpInside)

        return cell
    }


   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let productObject = productsObjectArray[indexPath.item]
        showSubViewInContainerViewProductCard(view: "ProductDetailView", object: [productObject], productsFilter: 0, marketProductsFilter: 0, menuFilter: 0, preparationFilter: 0, glutenFilter: 0,  backFromProductView: false)
    }   

@objc func rightSwiped(_ sender: UIGestureRecognizer)
    {
        let cell = sender.view as! FavoriteCollectionViewCell
        UIView.animate(withDuration: 0.4, delay: 0.1, options: .curveEaseInOut, animations: {
            cell.favoriteCellView.frame.origin.x = 0
        }) { (isCompleted) in
        }
    }

    @objc func leftSwiped(_ sender: UIGestureRecognizer)
    {
        let cell = sender.view as! FavoriteCollectionViewCell
        UIView.animate(withDuration: 0.4, delay: 0.1, options: .curveEaseInOut, animations: {
            cell.favoriteCellView.frame.origin.x = -50
        }) { (isCompleted) in
        }
    }

rightSwiped和leftSwiped函数允许您左右移动单元格。该代码可以正常工作。

我想做一个函数,当被调用时,它将关闭所有打开的单元格。因此,设置cell.favoriteCellView.frame.origin.x = 0。

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试

for cell in collectionView.visibleCells as! [FavoriteCollectionViewCell] {
    UIView.animate(withDuration: 0.4, delay: 0.1, options: .curveEaseInOut, animations: {
        cell.favoriteCellView.frame.origin.x = 0
    }) { (isCompleted) in
    }
}