ios - 将渐变应用于虚线 UIBezierPath

标签 ios gradient uibezierpath

这个渐变是我想在虚线 UIBezierPath 上实现的。
enter image description here

试过这段代码没有成功。

let gradient = CAGradientLayer()
gradient.frame = outerPath.bounds
gradient.colors = [UIColor.red.cgColor, UIColor.yellow.cgColor]

// mask
let shapeMask = CAShapeLayer()
shapeMask.path = outerPath.cgPath
self.insertSublayer(gradient, at: 0)

最佳答案

让 CAShapeLayer 做一个虚线 mask 。

class
TheView: UIView {

    required init?( coder aDecoder: NSCoder ) {
        super.init( coder: aDecoder )

        let outerPath = UIBezierPath( ovalIn: bounds.insetBy( dx: 20, dy: 20 ) )

        let gradient = CAGradientLayer()
        gradient.frame = bounds
        gradient.colors = [ UIColor.red.cgColor, UIColor.yellow.cgColor ]

        let shapeMask = CAShapeLayer()
        shapeMask.path = outerPath.cgPath
        shapeMask.lineWidth = 8
        shapeMask.lineDashPhase = 0
        shapeMask.lineDashPattern = [ 6, 2 ]
        shapeMask.lineCap = .butt
        shapeMask.lineJoin = .bevel
        shapeMask.strokeColor = UIColor.black.cgColor   //  Any color
        shapeMask.fillColor = nil
        gradient.mask = shapeMask
        layer.addSublayer( gradient )
    }
}

enter image description here

关于ios - 将渐变应用于虚线 UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51449611/

相关文章:

ios - 在 UITableView 后面设置渐变

gradient - 一侧的 CSS3 框阴影

ios - UIBezierPath 点的长度百分比

ios - 宽度动态变化的贝塞尔路径

objective-c - UIBezierPath 绘制的 View 的模糊屏幕截图

ios - 如何在 Xcode 中包含 git commit hash?

ios - 支持的视频格式 iOS7 UIImagePickerController

ios - Xcode 测试单独通过,与其他测试一起运行时失败

ios - 如何在 NSData 中存储 .flac 文件内容?

css - 跨浏览器渐变问题