gpt4 book ai didi

swift - 在 CAShapeLayer 上绘制 bezierPath 时上下文无效

转载 作者:搜寻专家 更新时间:2023-11-01 06:10:45 24 4
gpt4 key购买 nike

所以我在运行我的代码时遇到了这些错误:

CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

当我设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量时,错误指向我的 func bezierPath(dentInMM value: Float) -> CGPath 函数。以下是构成我的 Controller 这一部分的代码片段:

var shape = CAShapeLayer()

func someFunc() {
self.dentView.layer.addSublayer(self.shape)

let color: UIColor = .systemGreen
self.shape.fillColor = color.withAlphaComponent(0.3).cgColor
self.shape.strokeColor = color.cgColor
self.shape.lineWidth = 6
self.shape.path = self.bezierPath(dentInMM: 0)
}

func bezierPath(dentInMM value: Float) -> CGPath {
self.shape.frame = self.dentView.bounds

let x000 = Int(self.shape.frame.minX)
let x050 = Int(self.shape.frame.midX)
let x100 = Int(self.shape.frame.maxX)
let x010 = x100 / 10
let x040 = x050 - x010
let x060 = x050 + x010
let yOffSet = Int(self.shape.frame.minY)

let indentation = Int(value * self.dentDisplayMultiplier)

let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))
UIColor.label.setStroke()
bezierPath.stroke()

return bezierPath.cgPath
}

我需要在某处指定上下文吗?我的印象是 CAShapeLayer 提供了上下文......

最佳答案

不必调用绘图方法

bezierPath.stroke()

CAShapeLayer 会在 dentView 中绘制自己

你真的需要吗

UIColor.label.setStroke()

因为你已经在 someFunc() 中设置了 strokeColor

这是没有错误的工作:

class ThirdViewController: UIViewController {
let dentDisplayMultiplier : Float = 6
var shape = CAShapeLayer()

@IBOutlet weak var dentView: UIView!


func someFunc() {
self.dentView.layer.addSublayer(self.shape)

let color: UIColor = .systemGreen
self.shape.fillColor = color.withAlphaComponent(0.3).cgColor
self.shape.strokeColor = color.cgColor
self.shape.lineWidth = 6
self.shape.path = self.bezierPath(dentInMM: 3)
}

func bezierPath(dentInMM value: Float) -> CGPath {
self.shape.frame = self.dentView.bounds

let x000 = Int(self.shape.frame.minX)
let x050 = Int(self.shape.frame.midX)
let x100 = Int(self.shape.frame.maxX)
let x010 = x100 / 10
let x040 = x050 - x010
let x060 = x050 + x010
let yOffSet = Int(self.shape.frame.minY)

let indentation = Int(value * self.dentDisplayMultiplier)

let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))

return bezierPath.cgPath
}

override func viewDidLoad() {


super.viewDidLoad()
// Do any additional setup after loading the view.
someFunc()

}

}

关于swift - 在 CAShapeLayer 上绘制 bezierPath 时上下文无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58130232/

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