菜鸟教程小白 发表于 2022-12-11 21:46:54

ios - 如何在 Swift 中制作渐变圆形路径


                                            <p><p>我想创建一个如下图所示的渐变圆形路径:</p>

<p> <a href="/image/66iwr.png" rel="noreferrer noopener nofollow"><img src="/image/66iwr.png" alt="enter image description here"/></a> </p>

<p>我有以下代码:</p>

<pre><code>    circularPath = UIBezierPath(arcCenter: .zero, radius: radius, startAngle: 0, endAngle: .pi * 2, clockwise: true)
    outerTrackShapeLayer = CAShapeLayer()
    outerTrackShapeLayer.path = circularPath.cgPath
    outerTrackShapeLayer.position = position
    outerTrackShapeLayer.strokeColor = outerTrackColor.cgColor
    outerTrackShapeLayer.fillColor = UIColor.clear.cgColor
    outerTrackShapeLayer.lineWidth = lineWidth
    outerTrackShapeLayer.strokeEnd = 1
    outerTrackShapeLayer.lineCap = CAShapeLayerLineCap.round
    outerTrackShapeLayer.transform = rotateTransformation
    addSublayer(outerTrackShapeLayer)

    innerTrackShapeLayer = CAShapeLayer()
    innerTrackShapeLayer.strokeColor = innerTrackColor.cgColor
    innerTrackShapeLayer.position = position
    innerTrackShapeLayer.strokeEnd = progress
    innerTrackShapeLayer.lineWidth = lineWidth
    innerTrackShapeLayer.lineCap = CAShapeLayerLineCap.round
    innerTrackShapeLayer.fillColor = UIColor.clear.cgColor
    innerTrackShapeLayer.path = circularPath.cgPath
    innerTrackShapeLayer.transform = rotateTransformation
    let gradient = CAGradientLayer()
    gradient.frame = circularPath.bounds
    gradient.colors =
    gradient.position = innerTrackShapeLayer.position
    gradient.mask = innerTrackShapeLayer
    addSublayer(gradient)
</code></pre>

<p>但它不能正常工作,你可以在下图中看到结果:</p>

<p> <a href="/image/iveeN.png" rel="noreferrer noopener nofollow"><img src="/image/iveeN.png" alt="enter image description here"/></a> </p>

<p>如果有人帮助我,我将不胜感激,谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看起来渐变层框架设置为等于路径框架,不包括 CAShapeLayer 的笔划厚度,这就是为什么它被截断为正方形。我无法从代码中看到 subview 上是否有圆形路径,但是如果将渐变框架设置为与应该对截断进行排序的 subview 框架相同,以及轨道上进度 View 的错位.</p>

<p>希望对您有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Swift 中制作渐变圆形路径,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/54223709/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/54223709/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Swift 中制作渐变圆形路径