私はUIViewにグラデーションレイヤーを適用するためにUIView拡張機能を使用しています。私はランタイムにグラデーションの色を変更する必要があります。私は、scrollview contentoffset値を使ってグラデーションの色を更新します。 私はスーパーレイヤからレイヤを削除し、新しい色を持つ新しいgradientlayerを作成しようとしました:私が試した何iOS swiftで実行時にCAGradientLayerの色を更新するには?
。しかし、アプリはメモリの問題を抱えており、UIは時々凍っています。
実行時にCAGradientLayerのグラデーションカラーを更新することはできますか?
extension UIView {
func applyGradient(withColours colours: [UIColor], gradientOrientation orientation: GradientOrientation) {
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colours.map { $0.cgColor }
gradient.startPoint = orientation.startPoint
gradient.endPoint = orientation.endPoint
self.layer.insertSublayer(gradient, at: 0)
}
}
プロパティに勾配層を保持し、その色のプロパティを更新します。 – Alistra
はい。 @Alistraを動かしているようだ。ありがとう – abhi1992