gpt4 book ai didi

ios - 无法更改 swift 类循环加载器的属性

转载 作者:行者123 更新时间:2023-11-28 08:29:38 28 4
gpt4 key购买 nike

我使用本教程在 Swift 中创建了一个类来为圆圈加载设置动画 https://www.raywenderlich.com/94302/implement-circular-image-loader-animation-cashapelayer ,一切正常,但是当我尝试更改要在其中使用该类的 View Controller 中的笔触颜色时

class CircularLoaderView: UIView {
let circlePathLayer = CAShapeLayer()
var circleRadius: CGFloat = 20.0
var strokeColor = UIColor.whiteColor()


override init(frame: CGRect) {
super.init(frame: frame)
configure()
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
configure()
}

func configure() {
progress = 0
circlePathLayer.frame = bounds
circlePathLayer.lineWidth = 10
circlePathLayer.fillColor = UIColor.clearColor().CGColor
circlePathLayer.strokeColor = strokeColor.CGColor
layer.addSublayer(circlePathLayer)
backgroundColor = UIColor.clearColor()
}

然后我设置属性

//LoadingRing
progressIndicatorView.removeFromSuperview()
progressIndicatorView.frame = actionView.bounds
progressIndicatorView.circleRadius = (recordButtonWhiteRing.frame.size.height - 10) / 2
progressIndicatorView.progress = 0.0
progressIndicatorView.strokeColor = UIColor.init(red: 232 / 255.0, green: 28 / 255.0, blue: 45 / 255.0, alpha: 1)

但是笔划仍然显示为白色而不是所需的颜色,请帮忙!

最佳答案

笔划的颜色由 circlePathLayer.strokeColor 的值决定,您在创建进度指示器的实例时在 configure() 中修改该值 - 以及您的实例已创建,其 strokeColorUIColor.whiteColor()

您应该附加一个监听器以将 View 的 strokeColor 与其底层 circlePathLayer 同步:

var strokeColor = UIColor.whiteColor() {
didSet {
circlePathLayer.strokeColor = strokeColor.CGColor
}
}

或者只是使用 getters/setters 直接公开重要的 strokeColor

var strokeColor: UIColor {
get {
guard let cgColor = circlePathLayer.strokeColor else {
return UIColor.whiteColor()
}
return UIColor(CGColor: cgColor)
}
set (strokeColor) {
circlePathLayer.strokeColor = strokeColor.CGColor
}
}

(编辑:正如 Rob 指出的那样,CGColor 转换对于使其正确是必要的。)

关于ios - 无法更改 swift 类循环加载器的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089898/

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