CAGradientレイヤーを作成してうまく動作するUIViewに挿入するクラス関数があります。挿入後にCAGradientLayerの色を変更する
ただし、ページが表示された後にグラデーションを変更して、同じ機能を再実行しても機能しません。
表示されたページでは、スライダを使用して2つのUIButtonの背景に新しい色を設定し、以下の関数を呼び出します。スライダを変更したときにグラデーションレイヤの背景が変わることが予想されますが、スライダは変更されません。関数が呼び出され、色の値が有効であることを確認しました。
スライダを動かすと、どのようにグラデーションを変更できますか?
はここで、クラスの機能です:
class func setGradientBG(_ firstColor: CGColor, secondColor: CGColor, vertical: Bool, thisView: UIView){
let gradientLayer = CAGradientLayer()
gradientLayer.frame = thisView.bounds
gradientLayer.colors = [firstColor, secondColor]
gradientLayer.locations = [0.0, 1.2]
if !vertical{
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
}
thisView.layer.insertSublayer(gradientLayer, at: 0)
}
そして、ここでスライダーから呼び出される関数です:
func doSlider(){
CommonFuncs.setGradientBG((self.TopColorBut.backgroundColor?.cgColor)!, secondColor: (self.BottomColorBut.backgroundColor?.cgColor)!, vertical: true, thisView: self.view)
}
は、 'doSlider'メソッドが含まれているどのようなクラスthisView.layer.setNeedsDisplay() – Idali
を使うのか?グラデーションをいくつ追加していますか? 'setGradientBG'はなぜクラスメソッドですか? –