我使用本教程在Swift中创建了一个类来设置圆加载的动画
https://www.raywenderlich.com/94302/implement-circular-image-loader-animation-cashapelayer,一切都很好,但是当我试图在视图控制器中更改要在其中使用类的笔划的颜色时

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()
您应该附加一个侦听器来同步视图的strokeColor与其底层的circlePathLayer

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

或者只使用getter/setter直接公开重要的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类型转换是正确的ish所必需的。)

10-08 01:02