公告
财富商城
积分规则
提问
发文
2019-05-27 04:52发布
别忘想泡老子
How do I draw a line between cells in a UICollectionView that crosses over the spaces? The intended output is something like this.:
The best that I've done is to add lines inside each cell. How do I connect the lines through the spaces?
I could achieve this with below code, might have different approach too.
Ok, so I am creating a custom UIView with custom frame and just providing the frame blindly, you can calculate based on your adjacent cells.
UIView
frame
let cell1 = collectionView.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) let myView = UIView.init(frame: CGRectMake((cell1?.frame.origin.x)!, ((cell1?.frame.origin.y)! + 50.0), (cell1?.frame.size.width)!*4, 10)) myView.backgroundColor = UIColor.blueColor() collectionView.addSubview(myView) collectionView.bringSubviewToFront(myView)
This would draw a line of height 10.0. Let me know if this helps you.
height 10.0
I made an extension that you can use like this:
collectionView.drawLineFrom(indexPathA, to: indexPathB, color: UIColor.greenColor())
Here is the extension:
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) } }
The result looks like this:
最多设置5个标签!
I could achieve this with below code, might have different approach too.
Ok, so I am creating a custom
UIView
with customframe
and just providing theframe
blindly, you can calculate based on your adjacent cells.This would draw a line of
height 10.0
. Let me know if this helps you.I made an extension that you can use like this:
Here is the extension:
The result looks like this: