【发布时间】:2018-02-20 12:43:22
【问题描述】:
如何在 UIView 中添加虚线?
我的代码:
let path = UIBezierPath()
let p0 = CGPointMake(CGRectGetMinX(view.bounds),
CGRectGetMidY(view.bounds))
path.moveToPoint(p0)
let p1 = CGPointMake(CGRectGetMaxX(view.bounds),
CGRectGetMidY(view.bounds))
path.addLineToPoint(p1)
let dashes: [ CGFloat ] = [ 16.0, 32.0 ]
path.setLineDash(dashes, count: dashes.count, phase: 0.0)
path.lineWidth = 8.0
path.lineCapStyle = .Butt
UIColor.magentaColor().set()
path.stroke()
view.setNeedsDisplay()
但它不显示任何内容。
我在日志中得到了这个:
CGContextSetLineDash:无效的上下文 0x0。如果要查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。
【问题讨论】:
-
你能添加一个你想要创建的视图吗?
-
您是在
drawRect:函数中调用它吗?确保你这样做,然后删除view.setNeedsDisplay() -
Swift 2...哎呀...它已经过时了。
标签: ios swift uibezierpath drawrect