Ios 如何更改CAShapeLayer alpha/不透明度值?
无论我做什么,我似乎都无法改变我的Ios 如何更改CAShapeLayer alpha/不透明度值?,ios,swift,cashapelayer,Ios,Swift,Cashapelayer,无论我做什么,我似乎都无法改变我的CAShapeLayer的不透明度。我尝试设置不透明度: bottomProgressBar.strokeColor = UIColor.white.cgColor bottomProgressBar.opacity = 0.1 没用。然后我试了一下: bottomProgressBar.strokeColor = UIColor(displayP3Red: 255, green: 255, blue: 255, alpha: 0.1).cgC
CAShapeLayer
的不透明度。我尝试设置不透明度:
bottomProgressBar.strokeColor = UIColor.white.cgColor
bottomProgressBar.opacity = 0.1
没用。然后我试了一下:
bottomProgressBar.strokeColor = UIColor(displayP3Red: 255, green: 255, blue: 255, alpha: 0.1).cgColor
在这两种情况下,alpha值均为1。还有别的办法吗?还是我把它们设置错了 编辑: 这是我的代码:
这是从
UIView
继承的类:
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:
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:
我认为现在的情况是没有为要渲染的形状定义路径
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)
}
希望这有帮助。奇怪的是,它对您不起作用,因为
.opacity
改变了层的alpha
:
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 ***
我已经测试了这两种解决方案,都对我有用。你是否更改了正确的解决方案?我有一个进度条,带有底视图和顶视图,我的底视图的alpha应设置为0.1,但始终为1。两个视图的alpha都设置为1:/@布莱恩:我用我的代码更新了这个问题。是的,它被称为。如果我更改笔划的颜色,则工作正常。但在更改alpha值时不会。这可能是位置或动画问题吗?从代码上看,好像是
topProgressBar
被直接放在顶部。我用更多的代码更新了我的问题。实际上,我在layoutSubviews()中设置了路径。不确定它是否正常。在这种情况下,您的原始代码正在工作。是什么让你觉得不是?你能提供一个屏幕截图吗?编辑:例如,如果您设置topProgressBar.strokeEnd=0.5
,您将看到下划线层具有透明度.Done。正如你们所看到的,唯一不同的是底部有1磅的高度,顶部有2磅的高度。好的,你们是对的。我做了一个不同的项目来测试我的类,是的,不透明的工作。由于某种原因,它在我的应用程序中不起作用。哦,我刚刚解决了这个问题。故事板中添加的视图具有白色背景。它应该是透明的颜色。我现在才意识到。