【发布时间】:2023-10-22 02:57:05
【问题描述】:
所以我有一个用 UIBezierPath 绘制的圆圈,我需要将 superview 设置为与我的圆圈相同,这是我的代码:
override func draw(_ rect: CGRect) {
let circlePath = UIBezierPath(arcCenter: CGPoint(x: rect.midX, y: rect.midY), radius: circleRadius, startAngle: 0.0, endAngle:CGFloat(Double.pi * 2), clockwise: true)
let strokeColorAnimation = CABasicAnimation(keyPath: "strokeColor")
let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd")
shapeLayer.path = circlePath.cgPath
shapeLayer.lineWidth = 0.3 * circleRadius
shapeLayer.fillColor = UIColor.clear.cgColor
if oldColor == nil { oldColor = newColor }
if oldStrokeEnd == nil { oldStrokeEnd = 0.01 * currentCGFloatProximity }
strokeColorAnimation.fromValue = oldColor!.cgColor
strokeColorAnimation.toValue = newColor.cgColor
strokeColorAnimation.duration = 0.3
strokeColorAnimation.isRemovedOnCompletion = false
strokeColorAnimation.fillMode = kCAFillModeForwards
strokeEndAnimation.fromValue = oldStrokeEnd
strokeEndAnimation.toValue = 0.01 * currentCGFloatProximity
strokeEndAnimation.duration = 0.3
strokeEndAnimation.isRemovedOnCompletion = false
strokeEndAnimation.fillMode = kCAFillModeForwards
shapeLayer.add(strokeColorAnimation, forKey: strokeColorAnimation.keyPath)
shapeLayer.add(strokeEndAnimation, forKey: strokeEndAnimation.keyPath)
layer.mask = shapeLayer
// layer.addSublayer(shapeLayer)
oldColor = newColor
oldStrokeEnd = 0.01 * currentCGFloatProximity
}
当我将 shapeLayer 添加到 superview 的图层蒙版时,我得到了这个结果,它是黑色的,并且不改变它的颜色:
//layer.mask = shapeLayer
layer.addSublayer(shapeLayer)
我想要的只是第一张图片中的结果,但带有彩色圆圈。希望得到帮助。对不起我的英语。
【问题讨论】:
标签: swift calayer mask uibezierpath caanimation