0

我第一次在这里使用手势。请让我知道我的方法是否错误或任何更好的解决方案。

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

我正在使用/尝试此代码 更新了 viewDidLoad点击事件

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)
}

但它只有在轻扫单元格时才有效,而不是任何其他单元格或其他任何地方。任何的意见都将会有帮助!!

4

2 回答 2

0

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

let tap = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
self.view.addGestureRecognizer(tap)
于 2017-09-09T00:06:07.700 回答
0

最后,得到了解决方案。

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

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

于 2017-09-12T17:43:15.977 回答