美文网首页iOS 进阶UI
PZSwipedCollectionViewCell: UIC

PZSwipedCollectionViewCell: UIC

作者: 取水 | 来源:发表于2017-07-22 18:44 被阅读743次
swipelefttodelete.gif

UICollectionView 和 UITableView 不同,里面没有左滑删除功能。趁着休息,搭了一个库,给UICollectionView添加左滑删除功能,其实可以做更多事情。

PZSwipedCollectionViewCell 使用起来很方便:

  • 拖到工程里
  • cell 继承 PZSwipedCollectionViewCell

然后,就能使用左滑删除功能了。

要求: collectionView 的 width == mainScreen 的 width.

例子在这里 ,Objective-C 版本的我放到 gist 里了。


根据特性说原理。

cell 左滑显示删除按钮或者左滑显示自定义view

给 cell 添加 pan 手势,根据速度和方向判断是否是左滑,然后显示或者隐藏视图。

为了有效并且符合常规习惯,我们这里设定 UIGestureRecognizerDelegate 代理。

  • override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool 这个代理中设定是否要执行我们的pan手势。因为 pan 手势我们是加到 collectionVIew 里的,而在 collectionView 里也有 pan 手势,所以要加以区分。当 collectionView 上下滑动的时候,我们是不想触发 cell 的左滑删除功能的。因此,在这里判断横向速度和纵向速度。当横向速度>纵向速度时,说明它是左右滑动的,这时识别pan手势,否则的话就不识别。
  • func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool 多手势识别,这里用来判断当前手势是否是collectionView的pan,如果是就返回 false,否则返回 true
 // MARK: - UIGestureRecognizerDelegate
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer.isMember(of: UIPanGestureRecognizer.self) {
            let gesture:UIPanGestureRecognizer = gestureRecognizer as! UIPanGestureRecognizer
            let velocity = gesture.velocity(in: self)
            if fabs(velocity.x) > fabs(velocity.y) {
                return true
            }
        }
        return false
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return otherGestureRecognizer != self.superView(of: UICollectionView.self)
    }

根据 location 来判断什么时候显示或者隐藏。这些操作都是在 pan action 里进行的。然后,我们根据状态进行处理(began, changed, ended 等)。

在手势开始(began)的时候,获取 snapShot view ,然后把 revealView 、snapShotView 依次添加到cell里。然后我们操作 snapShotView 的 centerX 的位置来位移,用来展示或者隐藏revealView(删除按钮啥的)。

snapShotView 是当前cell的截屏,通过这个方法获得的:self.snapshotView(afterScreenUpdates: false)

在 changed 里来限定滑动的位置,确保左右滑动在 revealView width 范围里。

在 ended 里来判断是否显示、隐藏 revealView:

  • 向左滑动时
    • 滑动距离是否超过了 revealView width / 2 ,超过了显示,否则不显示
    • 是否向左滑动,根据速度的大小判断,小于0代表向左滑动(velocity.x < 0)。同时滑动速度是否满足能滑动完一般的距离(fabs(velocity.x) > revealView.frame.width / 2),不能的话原路返回。这两个条件,向左滑动同时能滑动完一般距离,同时满足时,显示revealView
  • 向右滑动,和向左滑动条件相反。

全局保持唯一的 opening cell

因为只是 cell 库,又想操作 collectionView 的属性。所以这里用了运行时,给 collectionView 添加了一个动态属性 pz_currentCell.

在每次开始( began )的时候,判断 super collectionView 里的运行时属性 pz_currentCell 是否等于自身,如果等于自身,说明是当前 cell 不做处理。如果不等于,两种情况,一种 pz_currentCell == nil,说明还没有值,则把自身赋值给它 pz_currentCell = self,如果有值,则执行关闭动画,关闭以前的 cell,然后再把自身赋值给它。

if superCollectionView.openingCell != self {
            if  superCollectionView.openingCell != nil {
                superCollectionView.openingCell!.hideRevealView(withAnimated: true)
            }
            superCollectionView.openingCell = self
}

参考:

相关文章

网友评论

    本文标题:PZSwipedCollectionViewCell: UIC

    本文链接:https://www.haomeiwen.com/subject/ihwukxtx.html