배경 UIView에서 내 앱에서 방사형 그라디언트를 사용하려고합니다. 그라디언트의 뷰 색상을 여러 번 업데이트하려는 경우 문제가 발생합니다. 내 코드에는 오류가 없지만이 문제를 해결하는 방법을 알아낼 수는 없습니다.UIView에서 방사형 그라디언트 색상이 업데이트되지 않습니다.
내가 뭘했는지 Grails 클래스뿐만 아니라 일반 UIView 내에서 입력보기를 다시로드하고있다; uiview의 서브 뷰를 제거하고, 화면에 새로운 뷰를 추가합니다.이 뷰는 설정된 색상의 변경에만 적용됩니다. 인터넷을 통해 살펴 봤지만 해결할 수없는 것 같습니다. 내가 원하는 건 UIView가 새로운 색상 매개 변수를 기반으로 색상을 업데이트하는 것입니다. 내가 그것을 사용하고 어디에
다음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)
}
}
은 다음과 같습니다 :
은 여기 내 반경 gradience 코드
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()
}
}
왜 항상 오래된 것을 제거한 다음 새 gradientview를 만드시겠습니까? 해당보기에서 색상을 변경하고 ** setNeedDisplay ** 메소드를 호출 할 수 있습니다. 이것은 뷰를 다시 그리는 필요성을 시스템에 알려줍니다. – Sergey