我在UIView的子视图中使用渐变,在从某些应用切换回应用后,渐变变为黑色。不知道为什么,如果有人知道这件事,请帮我在这里。渐变变成黑色
此外,我没有以编程方式继承它,直接把它放在故事板本身(CustomGradientBlueView)的UIView类中。这可能是一个问题。? 这是我使用了渐变的代码 - >
import UIKit
class CustomGradientBlueView: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
// colour declarations
let gradientColor1 = UIColor(red: 0.361, green: 0.145, blue: 0.553, alpha: 1.000)
let gradientColor2 = UIColor(red: 0.263, green: 0.537, blue: 0.635, alpha: 1.000)
// gradient decalarations
let gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [gradientColor1.CGColor,gradientColor2.CGColor], [0,1])
//rectangle drawing
var screen = UIScreen.mainScreen().bounds.size
let rectanglePath = UIBezierPath(rect: CGRectMake(0,0, screen.width, screen.height))
CGContextSaveGState(context)
rectanglePath.addClip()
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(screen.width, screen.height), 0)
}
}
为什么不使用CAGradientLayer? –
@MertBuran你能告诉我如何使用CAGradientLayer解决这个问题。有什么区别。? –
“通过从某些应用切换回应用后,渐变变为黑色”您能证明,当您这样做时,会调用'drawRect:'吗? – matt