バックグラウンドUIViewでアプリ内でラジアルグラデーションを使用しようとしています。グラディエントのビューカラーを複数回更新したいという私の問題が浮かび上がってきます。コードに間違いはありませんが、これを回避する方法を見つけることはできません。UIViewでラジアルグラデーションの色が更新されない
私が試みたのは、通常のUIViewとグラデーションクラスの両方で入力ビューをリロードすることでした。 uiviewのサブビューを削除し、設定された色を変更するだけの新しいビューを画面に追加する。私はインターネットを見てきましたが、これを解決できないようです。私が望むのは、UIViewが私が与える新しいカラーパラメータに基づいてその色を更新することだけです。ここで
は私のラジアルgradienceコードです:私はそれを使用していますのはここ
import UIKit
class RadialGradient: UIView {
var innerColor = UIColor.yellow
var outterColor = UIColor.red
override func draw(_ rect: CGRect) {
let colors = [innerColor.cgColor, outterColor.cgColor] as CFArray
let endRadius = min(frame.width, frame.height)
let center = CGPoint(x: bounds.size.width/2, y: bounds.size.height/2)
let gradient = CGGradient(colorsSpace: nil, colors: colors, locations: nil)
UIGraphicsGetCurrentContext()!.drawRadialGradient(gradient!,
startCenter: center,
startRadius: 0.0,
endCenter: center,
endRadius: endRadius,
options: CGGradientDrawingOptions.drawsAfterEndLocation)
}
}
は次のとおりです。
import UIKit
class TestIssuesVC: UIViewController {
var check : Bool = false
@IBAction func buttonClicked(_ sender: Any) {
if check == true {
backgroundsetting.removeFromSuperview()
print("Why wont you change to purple and black?????")
cheapFix(inner: UIColor.purple, outter: UIColor.black)
} else {
backgroundsetting.removeFromSuperview()
cheapFix(inner: UIColor.red, outter: UIColor.blue)
check = true
}
}
func cheapFix(inner: UIColor, outter: UIColor) {
let backgroundsetting = RadialGradient()
backgroundsetting.innerColor = inner
backgroundsetting.outterColor = outter
backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(backgroundsetting)
self.view.sendSubview(toBack: backgroundsetting)
self.reloadInputViews()
}
let backgroundsetting = RadialGradient()
override func viewWillAppear(_ animated: Bool) {
backgroundsetting.innerColor = UIColor.green
backgroundsetting.outterColor = UIColor.red
backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height))
self.view.addSubview(backgroundsetting)
self.view.sendSubview(toBack: backgroundsetting)
self.reloadInputViews()
}
}
いつも古い画像を削除してから新しい画像を作成するのはなぜですか?そのビューの色を変更し、** setNeedDisplay **メソッドを呼び出すことができます。これは、ビューを再描画する必要があることをシステムに通知します。 – Sergey