首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更改CAShapeLayer alpha/opacity值?

如何更改CAShapeLayer alpha/opacity值?
EN

Stack Overflow用户
提问于 2017-12-01 22:43:39
回答 2查看 3.8K关注 0票数 1

我似乎不能改变我的CAShapeLayer的不透明度,无论我做什么。我尝试设置不透明度:

代码语言:javascript
复制
    bottomProgressBar.strokeColor = UIColor.white.cgColor
    bottomProgressBar.opacity = 0.1

没有起作用。然后我试了一下:

代码语言:javascript
复制
bottomProgressBar.strokeColor = UIColor(displayP3Red: 255, green: 255, blue: 255, alpha: 0.1).cgColor

在这两种情况下,alpha值都是1。

有没有其他方法可以做到这一点?还是我把它们设置错了?

编辑:

下面是我的代码:

这是一个继承自UIView的类

代码语言:javascript
复制
override init(frame: CGRect) {
    super.init(frame: frame)
    configure()
}

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

func configure() {
    bottomProgressBar.strokeColor = UIColor.white.cgColor
    bottomProgressBar.opacity = 0.1
    bottomProgressBar.lineWidth = self.frame.height
    bottomProgressBar.strokeStart = 0
    bottomProgressBar.strokeEnd = 1
    self.layer.addSublayer(bottomProgressBar)

    topProgressBar.strokeColor = UIColor.white.cgColor
    topProgressBar.lineWidth = self.frame.height
    topProgressBar.strokeStart = 0
    topProgressBar.strokeEnd = 1
    self.layer.addSublayer(topProgressBar)
}

编辑2:

代码语言:javascript
复制
override func layoutSubviews() {
    super.layoutSubviews()

    let path = UIBezierPath()
    path.move(to: CGPoint(x: 0, y: 0))
    path.addLine(to: CGPoint(x: self.frame.width, y: 0))

    bottomProgressBar.path = path.cgPath
    topProgressBar.path = path.cgPath
}

编辑3:

EN

回答 2

Stack Overflow用户

发布于 2017-12-02 18:42:36

我认为发生的情况是,您没有为要渲染的形状定义路径。

代码语言:javascript
复制
    func configure() {
        let path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
        bottomProgressBar.frame = self.frame
        bottomProgressBar.bounds = self.bounds
        bottomProgressBar.path = path.cgPath
        bottomProgressBar.strokeColor = UIColor.red.cgColor
        bottomProgressBar.opacity = 0.1
//      no need for the following line as dimensions are already defined
//      bottomProgressBar.lineWidth = self.frame.height
//      bottomProgressBar.strokeStart = 0
//      bottomProgressBar.strokeEnd = 200
        self.layer.addSublayer(bottomProgressBar)

        // path width divided by 2 for demo
        let topPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width/2, height: self.frame.height))
        topProgressBar.frame = self.frame
        topProgressBar.bounds = self.bounds
        topProgressBar.path = topPath.cgPath
        topProgressBar.strokeColor = UIColor.green.cgColor
        self.layer.addSublayer(topProgressBar)
    }

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2020-07-05 04:51:46

奇怪的是它不能为你工作,因为是.opacity改变了我的layer's alpha

代码语言:javascript
复制
let circularPath = UIBezierPath(arcCenter: view.center,
                                          radius: (100,
                                          startAngle: -.pi/2,
                                          endAngle: 3 * .pi/2,
                                          clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 6

shapeLayer.opacity = 0.3 // *** fades out the shape layer ***
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47595807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档