在点击手势上重置 CollectionviewCell 位置

Posted

技术标签:

【中文标题】在点击手势上重置 CollectionviewCell 位置【英文标题】:Reset CollectionviewCell position on tap gesture 【发布时间】:2017-09-08 19:59:05 【问题描述】:

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

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

我正在使用/尝试此代码 更新了 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)

但它仅在轻按单元格时才有效,而不是任何其他单元格或其他任何地方。任何建议都会有所帮助!

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

谢谢雅各拉克!我试过了,它是一样的。只有当我点击选定的单元格时,单元格才会返回。 你要添加到哪个班级? 您好,抱歉,这里没有得到您要问的课程。我已经更新了上面的代码。请看一下。【参考方案2】:

终于找到了解决办法。

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

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

【讨论】:

以上是关于在点击手势上重置 CollectionviewCell 位置的主要内容,如果未能解决你的问题,请参考以下文章

SceneKit 相机节点在捏合手势时重置

在imageView依次加入7个手势, 1.点击哪个button,往imageView上加入哪个手势.(保证视图上仅仅有一个手势). 2.轻拍:点击视图切换美女图片.(imageView上首先

在 uiimageview 上无法识别点击手势

在collectionview中的uiimage上点击手势

iOS - 在图像视图上点击手势问题

如何在点击手势上为 UIImageView 设置动画?