在viewWillAppear方法上使用UIButton加载CAShapeLayer

时间:2018-05-14 10:28:26

标签: ios swift uibutton cashapelayer viewwillappear

我有一个带阴影效果的自定义按钮,我用来创建自定义按钮的类,我从stackoverflow获得。它使用CAShapeLayer来实现按钮上的阴影效果。

import UIKit

class CustomButton: UIButton {

var shadowLayer: CAShapeLayer!
override func layoutSubviews() {
    super.layoutSubviews()

    if shadowLayer == nil {
        shadowLayer = CAShapeLayer()
        shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 12).cgPath

        shadowLayer.fillColor = UIColor.white.cgColor
        shadowLayer.shadowColor = UIColor.darkGray.cgColor
        shadowLayer.shadowPath = shadowLayer.path
        shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
        shadowLayer.shadowOpacity = 0.8
        shadowLayer.shadowRadius = 2

        layer.insertSublayer(shadowLayer, at: 0)
        //layer.insertSublayer(shadowLayer, below: nil) // also works
    }
}
}

以下是一个屏幕图像,我使用这个CustomButton类创建了4个按钮,工作正常。

enter image description here

当点击四个以上的任何按钮时,我将其更改为以下属性,因此它看起来像一个活动按钮。

// to make button active
    func setSelectedButton(sender:CustomButton){
        //sender.backgroundColor = UIColor.red
        sender.shadowLayer.fillColor = UIColor(named: "headerColor")?.cgColor
        sender.setTitleColor(UIColor.white, for: .normal)
    }
// to make button inactive 
    func setUnselected(sender:CustomButton){
        //sender.backgroundColor = UIColor.init(red: 80/255, green:101/255, blue: 161/255, alpha: 1)
        sender.shadowLayer.fillColor = UIColor.white.cgColor
        sender.setTitleColor(.black, for: .normal)
    }

一切正常。现在我想要的是每当视图出现时我想要默认选择第一个按钮,这是例程按钮,我在viewWillAppearMethod中编写了以下代码

override func viewWillAppear(_ animated: Bool) {
        self.navigationItem.title = navigationTitle
        self.view.makeToastActivity(.center)
        self.routineButton.sendActions(for: .touchUpInside) //default selection of routineButton
        loadEvaluationList(userId: 8, evaluationType: "RT") {
            self.view.hideToastActivity()
        }
    }

self.routineButton.sendActions(for: .touchUpInside)执行时调用setSelectedButton(sender:CustomButton)方法,该方法在后续行

时出错
 sender.shadowLayer.fillColor = UIColor(named: "headerColor")?.cgColor

表示shadowLayer为零。仅当我尝试在viewWillAppear方法上设置默认选定按钮时才会出现此问题,否则它将工作为perfact。 我认为问题出现是因为在ViewWillAppear时没有初始化CustomButton的shadowLayer属性。 所以谁知道我该怎么办?它会有所帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

在init方法中移动shadowLayer的初始化代码,如下所示:

import UIKit

class CustomButton: UIButton {

    var shadowLayer: CAShapeLayer!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initShadowLayer()
    }

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

    func initShadowLayer() {
        if shadowLayer == nil {
            shadowLayer = CAShapeLayer()
            shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 12).cgPath

            shadowLayer.fillColor = UIColor.white.cgColor
            shadowLayer.shadowColor = UIColor.darkGray.cgColor
            shadowLayer.shadowPath = shadowLayer.path
            shadowLayer.shadowOffset = CGSize(width: 2.0, height: 2.0)
            shadowLayer.shadowOpacity = 0.8
            shadowLayer.shadowRadius = 2

            layer.insertSublayer(shadowLayer, at: 0)
        }

    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // Reset the path because bounds could have been changed
        shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 12).cgPath
        shadowLayer.shadowPath = shadowLayer.path
    }
}
在按钮viewWillAppear之前调用

layoutSubviewsshadowLayer尚未初始化。如果您只想配置按钮外观,请尽量不要拨打sendActions(for: .touchUpInside),而是调用setSelectedButton功能。