gpt4 book ai didi

swift - 重新加载绘制圆的值

转载 作者:行者123 更新时间:2023-11-30 12:12:08 25 4
gpt4 key购买 nike

您好,我正在创建一个半圆,它会根据我给您提供的始终变化的值来更新您的线宽。我想知道如何更新值以确定结束角度并且这些值始终会更新。谢谢

class Circle: UIView {  
//These 2 var (total dictionary and Secondary dictionary) have the number of the keys and the values ​​are always varying because they are obtained from json data
var dictionaryTotal: [String:String] = ["a":"153.23", "b":"162.45", "c":"143.65", "d":"140.78", "e":"150.23"]

var dictionarySecundario: [String:String] = ["w":"153.23", "l":"162.45", "v":"143.65"]

var lineWidth: CGFloat = 25

// circles radius
var half = CGFloat.pi
var quarter = CGFloat(3 * CGFloat.pi / 2)

func drawCircleCenteredAtVariavel(center:CGPoint, withRadius radius:CGFloat) -> UIBezierPath{
let circlePath = UIBezierPath(arcCenter: centerOfCircleView, radius: halfOfViewsSize, startAngle: half, endAngle: CGFloat(((CGFloat(self.dictionarySecundario.count) * CGFloat.pi) / CGFloat(self. dictionaryTotal.count)) + CGFloat.pi), clockwise: true)

circlePath.lineWidth = lineWidth

UIColor(red: 0, green: 0.88, blue: 0.70, alpha: 1).set()

return circlePath
}
override func draw(_ rect: CGRect) {
drawCircleCenteredAtVariavel(center: centerOfCircleView, withRadius: halfOfViewsSize).stroke()

}

}

This is the picture of semicircle

最佳答案

从你的问题中不清楚你想要完成什么,所以这不是真正的答案,但它比我想输入的评论要多。

您应该做的第一件事是分离职责,因此上面的Circle类应该只负责绘制特定配置的半圆。

例如,基于上面代码的半圆形进度 View :

class CircleProgressView: UIView {
var lineWidth: CGFloat = 25
var barColor = UIColor(red: 0, green: 0.88, blue: 0.70, alpha: 1)
var progress: Float = 0
{
didSet {
progress = max(min(progress, 1.0), 0.0)
setNeedsDisplay()
}
}

func semicirclePath(at center: CGPoint, radius: CGFloat, percentage: Float) -> UIBezierPath {
let endAngle = CGFloat.pi + (CGFloat(percentage) * CGFloat.pi)
let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat.pi, endAngle: endAngle, clockwise: true)

circlePath.lineWidth = lineWidth

return circlePath
}

override func draw(_ rect: CGRect) {
barColor.set()

let center = CGPoint.init(x: bounds.midX, y: bounds.midY)
let radius = max(bounds.width, bounds.height)/2 - lineWidth/2
semicirclePath(at: center, radius: radius, percentage: progress).stroke()
}
}

然后,在此类之外,您还可以使用其他代码来根据您收到的 JSON 值确定进度

关于swift - 重新加载绘制圆的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45925866/

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