Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在一个上下文中绘制实线和虚线_Swift_Core Graphics - Fatal编程技术网

Swift 在一个上下文中绘制实线和虚线

Swift 在一个上下文中绘制实线和虚线,swift,core-graphics,Swift,Core Graphics,我想在一个上下文中画一条虚线和一条实线。但是如果我先画一条虚线,第二条线也是一条虚线。那么如何清理context.setLineDash?我尝试了context.beginContext(),但没有帮助 代码如下: if let context = UIGraphicsGetCurrentContext() { // The first line let startPoint = CGPoint(x: 20, y: rect.height/2) let endPoint

我想在一个上下文中画一条虚线和一条实线。但是如果我先画一条虚线,第二条线也是一条虚线。那么如何清理context.setLineDash?我尝试了
context.beginContext()
,但没有帮助

代码如下:

if let context = UIGraphicsGetCurrentContext() {
    // The first line
    let startPoint = CGPoint(x: 20, y: rect.height/2)
    let endPoint = CGPoint(x: rect.width - 40, y: rect.height/2)
    context.move(to: startPoint)
    context.addLine(to: endPoint)
    context.setLineWidth(1)
    context.setLineDash(phase: 0, lengths: [4, 4])
    context.setStrokeColor(UIColor.progressDashLineColor.cgColor)
    context.strokePath()
    // Second line
    let startPoint1 = CGPoint(x: 20, y: rect.height/2 + 5)
    let endPoint1 = CGPoint(x: rect.width-120, y: rect.height/2)
    context.setStrokeColor(UIColor.mainColor.cgColor)
    context.setLineWidth(5)
    context.move(to: startPoint1)
    context.addLine(to: endPoint1)
    context.setLineCap(.round)
    context.strokePath()
}

在绘制实线之前,将虚线设置为空数组

context.setLineDash(phase: 0, lengths: [])
CGContext
文档中:

传递一个空数组以清除破折号图案,以便所有笔划 上下文中的图形使用实线