如何在单击按钮时更改 CAShaperLayer 的描边颜色
How to change stroke color of CAShaperLayer on button click
我已经创建了一些自定义层并添加到 UIView 上,我有两个 CAShaper 层,但我在这里只显示一个示例
hexBorderOtline = CAShapeLayer()
hexBorderOtline.name = "dhiru_border_hidden"
hexBorderOtline.path = path2.cgPath
hexBorderOtline.lineCap = .round
hexBorderOtline.strokeColor = inActiveBorderColor.cgColor
hexBorderOtline.fillColor = UIColor.clear.cgColor
self.layer.addSublayer(hexBorderOtline)
我想在单击按钮时更改它的边框颜色。
func btnAction()
{
hexBorderOtline.strokeColor = activeBorderColor.cgColor
}
但这不起作用,我正在发布一张参考图片,我需要在单击按钮时执行此操作。
试试这个:
func btnAction()
{
hexBorderOtline.removeFromSuperlayer()
hexBorderOtline.strokeColor = activeBorderColor.cgColor
self.layer.addSublayer(hexBorderOtline)
}
没有 UIView:
class ViewController: UIViewController {
var hexBorderOtline: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
hexBorderOtline = CAShapeLayer()
hexBorderOtline.name = "dhiru_border_hidden"
hexBorderOtline.path = UIBezierPath(arcCenter: CGPoint(x:self.view.frame.size.width/2, y: self.view.frame.size.height/2), radius: self.view.frame.size.height/2, startAngle: 180, endAngle: 0.0, clockwise: true).cgPath
hexBorderOtline.lineCap = .round
hexBorderOtline.strokeColor = UIColor.gray.cgColor
hexBorderOtline.fillColor = UIColor.clear.cgColor
self.view.layer.addSublayer(hexBorderOtline)
}
@IBAction func updateColor(_ sender: Any) {
hexBorderOtline.removeFromSuperlayer()
hexBorderOtline.strokeColor = UIColor.red.cgColor
self.view.layer.addSublayer(hexBorderOtline)
}
}
如果您只有 2 个层,用颜色显示活动和非活动模式,您可以创建两个具有不同描边颜色的单独层,并将它们作为子层添加到主层中。您要显示的默认图层颜色应在之后添加。单击按钮后,您只需要 hide/show 个子层就可以了。
希望对您有所帮助。
您切换形状图层描边颜色的方法是正确的:
我所做的只是:
var enabled = false
@IBAction func didTapButton(_ sender: Any) {
enabled = !enabled
hexBorderOtline.strokeColor = enabled ? activeBorderColor.cgColor : inActiveBorderColor.cgColor
}
那么,您的问题出在别处。例如:
也许您在要更改颜色的图层之上还有另一个形状图层。
或者您的 ivar 可能指向另一个形状图层。
或者你的按钮可能没有连接到你的 btnAction
例程(没有任何 @IBAction
或 @objc
让我怀疑你没有调用此例程)。
就这么简单。
我建议您在创建 CAShapeLayer
的地方添加 print(hexBorderOtline)
,然后在 btnAction
中再次添加,并确认两者:
您看到了两组 print
语句;和
它们正在打印与形状层关联的相同内存地址(即确保它们指的是相同的形状层)。
但它必须是那样的。这是更改 strokeColor
的正确方法,它会自动为您更新形状图层。你的问题出在别处。
我已经创建了一些自定义层并添加到 UIView 上,我有两个 CAShaper 层,但我在这里只显示一个示例
hexBorderOtline = CAShapeLayer()
hexBorderOtline.name = "dhiru_border_hidden"
hexBorderOtline.path = path2.cgPath
hexBorderOtline.lineCap = .round
hexBorderOtline.strokeColor = inActiveBorderColor.cgColor
hexBorderOtline.fillColor = UIColor.clear.cgColor
self.layer.addSublayer(hexBorderOtline)
我想在单击按钮时更改它的边框颜色。
func btnAction()
{
hexBorderOtline.strokeColor = activeBorderColor.cgColor
}
但这不起作用,我正在发布一张参考图片,我需要在单击按钮时执行此操作。
试试这个:
func btnAction()
{
hexBorderOtline.removeFromSuperlayer()
hexBorderOtline.strokeColor = activeBorderColor.cgColor
self.layer.addSublayer(hexBorderOtline)
}
没有 UIView:
class ViewController: UIViewController {
var hexBorderOtline: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
hexBorderOtline = CAShapeLayer()
hexBorderOtline.name = "dhiru_border_hidden"
hexBorderOtline.path = UIBezierPath(arcCenter: CGPoint(x:self.view.frame.size.width/2, y: self.view.frame.size.height/2), radius: self.view.frame.size.height/2, startAngle: 180, endAngle: 0.0, clockwise: true).cgPath
hexBorderOtline.lineCap = .round
hexBorderOtline.strokeColor = UIColor.gray.cgColor
hexBorderOtline.fillColor = UIColor.clear.cgColor
self.view.layer.addSublayer(hexBorderOtline)
}
@IBAction func updateColor(_ sender: Any) {
hexBorderOtline.removeFromSuperlayer()
hexBorderOtline.strokeColor = UIColor.red.cgColor
self.view.layer.addSublayer(hexBorderOtline)
}
}
如果您只有 2 个层,用颜色显示活动和非活动模式,您可以创建两个具有不同描边颜色的单独层,并将它们作为子层添加到主层中。您要显示的默认图层颜色应在之后添加。单击按钮后,您只需要 hide/show 个子层就可以了。
希望对您有所帮助。
您切换形状图层描边颜色的方法是正确的:
我所做的只是:
var enabled = false
@IBAction func didTapButton(_ sender: Any) {
enabled = !enabled
hexBorderOtline.strokeColor = enabled ? activeBorderColor.cgColor : inActiveBorderColor.cgColor
}
那么,您的问题出在别处。例如:
也许您在要更改颜色的图层之上还有另一个形状图层。
或者您的 ivar 可能指向另一个形状图层。
或者你的按钮可能没有连接到你的
btnAction
例程(没有任何@IBAction
或@objc
让我怀疑你没有调用此例程)。
就这么简单。
我建议您在创建 CAShapeLayer
的地方添加 print(hexBorderOtline)
,然后在 btnAction
中再次添加,并确认两者:
您看到了两组
print
语句;和它们正在打印与形状层关联的相同内存地址(即确保它们指的是相同的形状层)。
但它必须是那样的。这是更改 strokeColor
的正确方法,它会自动为您更新形状图层。你的问题出在别处。