gpt4 book ai didi

swift - 将图层蒙版设置为 UIBezierPath super View

转载 作者:搜寻专家 更新时间:2023-11-01 07:09:35 26 4
gpt4 key购买 nike

所以我有一个用 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 的图层蒙版时,我得到了这个结果,它是黑色的并且没有改变它的颜色: enter image description here

    //layer.mask = shapeLayer

layer.addSublayer(shapeLayer)

当我将子层添加到 superview 的层时,我得到了这个结果: enter image description here

我想要的只是第一张图片中的结果,但带有彩色圆圈。希望得到帮助。对不起我的英语。

最佳答案

好的,我通过添加新的形状图层修复了它,但也许有人有更好的解决方案。现在我有了这段代码:

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")

circleLayer.path = circlePath.cgPath
circleLayer.lineWidth = 0.3 * circleRadius
circleLayer.fillColor = UIColor.clear.cgColor

backgroundLayer.path = circlePath.cgPath
backgroundLayer.lineWidth = 0.3 * circleRadius
backgroundLayer.strokeColor = UIColor.black.cgColor
backgroundLayer.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

circleLayer.add(strokeColorAnimation, forKey: strokeColorAnimation.keyPath)
circleLayer.add(strokeEndAnimation, forKey: strokeEndAnimation.keyPath)

layer.mask = backgroundLayer

layer.addSublayer(circleLayer)
oldColor = newColor
oldStrokeEnd = 0.01 * currentCGFloatProximity
}

Result

关于swift - 将图层蒙版设置为 UIBezierPath super View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45733473/

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