在点击手势时重置 CollectionviewCell 位置

Reset CollectionviewCell position on tap gesture

我是第一次在这里使用手势。如果我的方法有误或有更好的解决方案,请告诉我。

我正在尝试像 UITableview 删除功能一样在向左滑动时删除 collectionView Cell。删除工作正常。现在我想要的是,一旦我滑动单元格并点击 COllectionView 上的任意位置,它应该滑动回到其原始位置(与 tableview 删除行功能相同)

我是using/trying这段代码 更新了 viewDidLoadtapped 事件

override func viewDidLoad() {
super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
        self.view.addGestureRecognizer(tap)
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CustomCell
    Cell.backgroundColor = UIColor.white

    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(delete(sender:)))
    leftSwipe.direction = UISwipeGestureRecognizerDirection.left
    Cell.addGestureRecognizer(leftSwipe)

    let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
    Cell.addGestureRecognizer(tap)

    Cell.deleteButton.addTarget(self, action: #selector(DeleteCell(sender:)), for: .touchUpInside)
}

func tapped(_ recognizer: UITapGestureRecognizer) {
   // self.collectionView.performBatchUpdates({ 
        //self.collectionView.reloadSections(NSIndexSet(index: 0) as IndexSet)
    //}, completion: nil)


    let point = recognizer.location(in: collectionView)
    let indexPath = collectionView.indexPathForItem(at: point)        
    let cell = self.collectionView.cellForItem(at: indexPath!)

    UIView.animate(withDuration: 0.4) {

        cell?.contentView.frame = CGRect(x: 0, y: 0, width: (cell?.contentView.frame.width)!, height: (cell?.contentView.frame.height)!)
    }

}

func delete(sender: UISwipeGestureRecognizer){

    let cell = sender.view as! CustomCell

    UIView.animate(withDuration: 0.4) {
        cell.contentView.frame = CGRect(x: -90, y: 0, width: cell.contentView.frame.width, height: cell.contentView.frame.height)
    }
}

func DeleteCell(sender : AnyObject){

    let cell = sender.superview as! CustomCell
    let i = self.collectionView.indexPath(for: cell)!.item

    let indexpath = self.collectionView.indexPath(for: cell)
    let array : NSMutableArray = []

    self.collectionView.performBatchUpdates({ 

        self.userArray.remove(at: i)

        array.add(indexpath!)

        self.collectionView.deleteItems(at:array as! [IndexPath])

    }, completion: nil)
}


class CustomCell: UICollectionViewCell {
    let deleteButton: UIButton = {
    let deleteBtn = UIButton()
    deleteBtn.setImage(UIImage(named: "red"), for: .normal)
    deleteBtn.contentMode = .scaleAspectFit
    return deleteBtn
    }()
}

所以在这里我可以通过 self.collectionView.performBatchUpdates 将单元格的位置设置回原始位置,但它的动画并不流畅。我尝试使用

UIView.animate(withDuration: 0.4) {            
    cell.contentView.frame = CGRect(x: 0, y: 0, width:     cell.contentView.frame.width, height: cell.contentView.frame.height)
}

但它仅在轻触滑动单元格时有效,在任何其他单元格或其他任何地方均无效。任何建议都会有所帮助!!

现在您正在从内部访问您的手机。它仅适用于点击您刚刚滑动的单元格的原因是因为这是唯一具有 UITapGestureRecognizer 特定实例的单元格。要解决此问题,您应该将该点击手势识别器添加到整个视图中。尝试将此添加到您的 viewDidLoad() 方法中:

let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
self.view.addGestureRecognizer(tap)

终于找到解决办法了。

这是我找到的演示项目 - CollectionViewSlideLeft

希望它能帮助像我这样的人。 :)