gpt4 book ai didi

ios - CAShapeLayer 意外的 StrokeEnd

转载 作者:行者123 更新时间:2023-11-28 05:40:04 25 4
gpt4 key购买 nike

<分区>

我正在使用 CAShapeLayer 绘制一个圆圈,由于某种原因,笔画只会以 25% 的间隔绘制。例如,将 StrokeEnd 设置为 0.4 会将笔划绘制到圆的 50%。

如何将 CAShapeLayer 的笔触绘制到预期位置?

import UIKit

class DrawCircle {
let shapeLayer = CAShapeLayer()
let trackLayer = CAShapeLayer()
let center: CGPoint
let radius: CGFloat
let startAngle: CGFloat
let endAngle: CGFloat
let clockwise: Bool

let fillColor: CGColor
let strokeColor: CGColor
let lineWidth: CGFloat
var strokeEnd: CGFloat
let view: UIView
let circularPath: UIBezierPath
let withTrack: Bool
private var trackColor: CGColor

init(withTrack: Bool, trackColor: CGColor, radius: CGFloat, center: CGPoint, clockwise: Bool, fillColor: CGColor, strokeColor: CGColor, lineWidth: CGFloat, view: UIView) {
//path params
self.center = center
self.radius = radius
self.startAngle = -CGFloat.pi / 2
self.endAngle = 2 * CGFloat.pi
self.clockwise = clockwise
self.circularPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: clockwise)
//shape params
self.fillColor = fillColor
self.strokeColor = strokeColor
self.strokeEnd = 0.4
self.lineWidth = lineWidth

//view to attach to
self.view = view
self.withTrack = withTrack
if withTrack {
self.trackColor = trackColor
} else {
self.trackColor = UIColor.clear.cgColor
}
}

func draw() {
if withTrack {
drawTrack()
print("track drawn: \(self.trackLayer)")
}
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = strokeColor
shapeLayer.fillColor = fillColor
shapeLayer.lineWidth = self.lineWidth
shapeLayer.strokeEnd = self.strokeEnd
shapeLayer.lineCap = .round
view.layer.addSublayer(shapeLayer)
}

func drawTrack() {
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = trackColor
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineWidth = self.lineWidth
trackLayer.lineCap = .round
view.layer.addSublayer(trackLayer)
}

}

这个问题被标记为重复,但问题完全不同,提供的答案画了半个圆圈,而不是一个完整的圆圈。结束角度值完全不同

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