UICollectionView - 在单元格之间画一条线

2024-04-20

如何在 UICollectionView 中的单元格之间绘制一条跨越空间的线? 预期的输出是这样的:

我所做的最好的事情就是在每个单元格内添加行。如何连接穿过空间的线?


我做了一个扩展,你可以像这样使用:

collectionView.drawLineFrom(indexPathA, to: indexPathB, color: UIColor.greenColor())

这是扩展:

extension UICollectionView {

    func drawLineFrom(
        from: NSIndexPath,
        to: NSIndexPath,
        lineWidth: CGFloat = 2,
        strokeColor: UIColor = UIColor.blueColor()
    ) {
        guard
            let fromPoint = cellForItemAtIndexPath(from)?.center,
            let toPoint = cellForItemAtIndexPath(to)?.center
        else {
            return
        }

        let path = UIBezierPath()

        path.moveToPoint(convertPoint(fromPoint, toView: self))
        path.addLineToPoint(convertPoint(toPoint, toView: self))

        let layer = CAShapeLayer()

        layer.path = path.CGPath
        layer.lineWidth = lineWidth
        layer.strokeColor = strokeColor.CGColor

        self.layer.addSublayer(layer)
    }
}

结果如下:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UICollectionView - 在单元格之间画一条线 的相关文章

随机推荐