【发布时间】:2015-05-28 12:36:53
【问题描述】:
我在 UIView 的子视图中使用渐变,从某些应用切换回到应用后,渐变变为黑色。不知道为什么,如果有人知道这件事,请在这里帮助我。
我也没有以编程方式对其进行子类化,直接将其放在 Storyboard 本身的 UIView 的类位置(CustomGradientBlueView)。这可能是一个问题。? 这是用于渐变的代码 ->
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 我正在使用这个应用程序,然后我切换到 Facebook 应用程序(可以说任何应用程序)。用Facebook很久了,回到我的app测试,突然变黑了(是起始页)
-
你没有回答我问的问题。
标签: ios objective-c swift uiview