gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:30:32 30 4
gpt4 key购买 nike

编辑:抱歉,我最初并不清楚。我想获得线条或形状的“轮廓”路径。我特别想了解如何使用:

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()

最佳答案

类稍作修改以生成此图形:

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()

}

}

关于swift - 如何在 Swift Playground 中调用 UIBezier 大纲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856621/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com