用Swift实现CollectionView滑动手势删除项目

3

我有一个水平滚动的集合视图。

我正在寻找一种简洁的方法来使用向上或向下滑动手势删除项目。
同时重新排列元素也会很棒,但目前删除更为重要。

我找到了一些Obj-C文档,但由于我对Swift Obj-C还不熟悉,这对我来说太难了。


到目前为止,你进展如何?你能够抓取一个物品并移动它吗,即使更改没有保存? - Wain
1
这个非常相关的问题有Objective-C的答案,但是你应该能够轻松地为那里列出的几个API想出Swift的等效方法... - Michael Dautermann
2个回答

7
我已经处理了几天相同的情况。这是我用Swift做的... 我检查了Michael的链接并进行了一些研究...

所以...

添加这个

    let cSelector = Selector("reset:")
    let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector )
    UpSwipe.direction = UISwipeGestureRecognizerDirection.Up
    cell.addGestureRecognizer(UpSwipe)

要实现这个功能,你需要在你的collectionView delegate方法中,添加下面的代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

接着,你需要定义一个选择器(selector),用来从你的数组中删除滑动的项目,然后重新加载你的collection view。

    func reset(sender: UISwipeGestureRecognizer) {
        let cell = sender.view as! UICollectionViewCell
        let i = self.favoritesCV.indexPathForCell(cell)!.item
        favoritesInstance.favoritesArray.removeAtIndex(i)  //replace favoritesInstance.favoritesArray with your own array
        self.favoritesCV.reloadData() // replace favoritesCV with your own collection view.
    }

1
非常感谢。我其实做了类似的事情,但您的解决方案更为简洁。 - Cory Trentini
你也可以创建手势识别器的子类并传递indexPath。 - Koray Birand

0

你也可以通过在单元格中使用多个视图来实现它。

这是我的代码。首先使用三个视图。

示例:

@IBOutlet weak var YOURVIEW: UIView!
@IBOutlet weak var edit: UIView!
@IBOutlet weak var delete: UIView!

现在对YOURVIEW的前导和尾随进行布局

@IBOutlet weak var YOURLEADING: NSLayoutConstraint!
@IBOutlet weak var YOURTRAILING: NSLayoutConstraint!

将以下内容添加到重写的 func awakeFromNib() 中:

let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeLeft.direction = .left
            self.YOURTOPVIEW.addGestureRecognizer(swipeLeft
            )

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture))
        swipeRight.direction = .right
            self.YOURTOPVIEW.addGestureRecognizer(swipeRight)

现在在类体中编写这段代码

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {

        if let swipeGesture = gesture as? UISwipeGestureRecognizer {

            switch swipeGesture.direction {
            
            case .left:
                self.animate()
                self.YOURLEADING.constant = -100
                self.YOURTRAILING.constant = 100
               // YOUR OTHER ACTIONS HERE

                
            case .right:
                self.animate()
                self.YOURLEADING.constant = 100
                self.YOURTRAILING.constant = -100
              // YOUR OTHER ACTIONS HERE
               
            default:
                break
            }
        }
    }

同时编写一个函数来展示动画效果

func animate()
    {
        UIView.animate(withDuration: 1,
                       delay: 0.0,
                                   animations: { () -> Void in
                                    self.YOURTOPVIEW.frame = CGRect(x: 0, y: 0, width: self.YOURTOPVIEW.frame.width, height: self.YOURTOPVIEW.frame.height)

        }, completion: { (finished: Bool) -> Void in })
    }

现在手势识别器将在特定视图上工作,并且看起来就像您在滑动集合视图单元格一样。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接