如何在Swift Playground中调用UIBezier大纲?

时间:2019-02-24 21:14:06

标签: swift uibezierpath

编辑:对不起,我本来不清楚。我想获得直线或形状的“轮廓”路径。我正在专门尝试了解如何使用:

context.replacePathWithStrokedPath()

和/或:

CGPathRef CGPathCreateCopyByStrokingPath(CGPathRef path, const CGAffineTransform *transform, CGFloat lineWidth, CGLineCap lineCap, CGLineJoin lineJoin, CGFloat miterLimit);

https://developer.apple.com/documentation/coregraphics/1411128-cgpathcreatecopybystrokingpath?language=objc

谢谢,我不是在寻找解决方法。

=====

我真的是想把自己的头围成一条带有轮廓的线。我正在使用UIBezier,但遇到了砖墙。到目前为止,我已经知道了:

import UIKit
import PlaygroundSupport

let screenWidth = 375.0 // points
let screenHeight = 467.0 // points

let centerX = screenWidth / 2.0
let centerY = screenHeight / 2.0

let screenCenterCoordinate = CGPoint(x: centerX, y: centerY)

class LineDrawingView: UIView {
override func draw(_ rect: CGRect) {
    let path = UIBezierPath()
    path.lineWidth = 5
    path.lineCapStyle = .round

    //Move to Drawing Point
    path.move(to: CGPoint(x:20, y:120))
    path.addLine(to: CGPoint(x:200, y:120))

    path.stroke()

    let dot = UIBezierPath()
    dot.lineWidth = 1
    dot.lineCapStyle = .round

    dot.move(to: CGPoint(x:200, y:120))
    dot.addArc(withCenter: CGPoint(x:200, y:120), radius: 5, startAngle: CGFloat(0.0), endAngle: CGFloat(8.0), clockwise: true)

    UIColor.orange.setStroke()
    UIColor.orange.setFill()
    path.stroke()
    dot.fill()

    let myStrokedPath = UIBezierPath.copy(path)

    myStrokedPath().stroke()

  }

}


let tView = LineDrawingView(frame: CGRect(x: 0,y: 0, width: screenWidth, height: screenHeight))
tView.backgroundColor = UIColor.white

PlaygroundPage.current.liveView = tView

那么,我在哪里出错呢?我似乎无法弄清楚在哪里使用CGPathCreateCopyByStrokingPath ...或如何...

编辑2:

好,现在我明白了。言归正传,但是我该如何再次填充路径?

    let c = UIGraphicsGetCurrentContext()!

    c.setLineWidth(15.0)

    let clipPath = UIBezierPath(arcCenter: CGPoint(x:centerX,y:centerY), radius: 90.0, startAngle: -0.5 * .pi, endAngle: 1.0 * .pi, clockwise: true).cgPath

    c.addPath(clipPath)
    c.saveGState()
    c.replacePathWithStrokedPath()

    c.setLineWidth(0.2)
    c.setStrokeColor(UIColor.black.cgColor)
    c.strokePath()

2 个答案:

答案 0 :(得分:0)

对该类进行了少许修改以生成此图形:

OrangeLineWithOutline

未在修改的代码中复制路径。取而代之的是使用现有路径进行绘制,然后进行修改和重用。点没有笔画,因此添加了笔画。由于只能填充闭合的路径,因此我通过更改线宽在较粗的路径上绘制了较细的路径。

这是修改后的代码:

class LineDrawingView: UIView {
  override func draw(_ rect: CGRect) {
    let path = UIBezierPath()
    path.lineWidth = 7
    path.lineCapStyle = .round

    //Move to Drawing Point
    path.move(to: CGPoint(x:20, y:120))
    path.addLine(to: CGPoint(x:200, y:120))

    path.stroke()

    let dot = UIBezierPath()
    dot.lineWidth = 1
    dot.lineCapStyle = .round

    dot.move(to: CGPoint(x:200, y:120))
    dot.addArc(withCenter: CGPoint(x:200, y:120), radius: 5, startAngle: CGFloat(0.0), endAngle: CGFloat(8.0), clockwise: true)

    dot.stroke()

    UIColor.orange.setStroke()
    UIColor.orange.setFill()
    path.lineWidth = 5
    path.stroke()
    dot.fill()

  }

}

答案 1 :(得分:0)

因此,我找到了一个答案。我使用了CAShapeLayer:

let c = UIGraphicsGetCurrentContext()!

    c.setLineCap(.round)
    c.setLineWidth(15.0)

    c.addArc(center: CGPoint(x:centerX,y:centerY), radius: 90.0, startAngle: -0.5 * .pi, endAngle: (-0.5 * .pi) + (3 / 2 * .pi ), clockwise: false)

    c.replacePathWithStrokedPath()

    let shape = CAShapeLayer()
    shape.path = c.path
    shape.fillColor = UIColor.yellow.cgColor
    shape.strokeColor = UIColor.darkGray.cgColor
    shape.lineWidth = 1

    myView.layer.addSublayer(shape)

它足够好,但不能在重叠的图层上使用。我需要学习如何连接轮廓或其他东西。