我是Swift新手,我嘗試使用CGGradient創建背景漸變,但出現以下錯誤。無法將int類型的值轉換爲預期的參數類型CGGradientDrawingOptions
不能轉換int類型的值與預期的參數類型CGGradientDrawingOptions
我不知道爲什麼我不應該在這裏使用一個int。任何幫助將不勝感激。
override func viewDidLoad() {
super.viewDidLoad()
var currentContext = UIGraphicsGetCurrentContext()
CGContextSaveGState(currentContext);
var colorSpace = CGColorSpaceCreateDeviceRGB()
var topColor = UIColor(red: (57/255.0), green: (150/255.0), blue: (199/255.0), alpha: 1)
var startColorComponents = CGColorGetComponents(topColor.CGColor)
var middleColor = UIColor(red: (90/255.0), green: (60/255.0), blue: (117/255.0), alpha: 0.85)
var middleColorComponents = CGColorGetComponents(middleColor.CGColor)
var bottomColor = UIColor(red: (215/255.0), green: (109/255.0), blue: (109/255.0), alpha: 1)
var endColorComponents = CGColorGetComponents(bottomColor.CGColor)
var colorComponents
= [startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3],
middleColorComponents[0], middleColorComponents[1], middleColorComponents[2], middleColorComponents[3],
endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3]
]
var locations:[CGFloat] = [0.0, 0.35, 1.0]
var gradient = CGGradientCreateWithColorComponents(colorSpace,&colorComponents,&locations,3)
var startPoint = CGPointMake(-0.3, 0.1)
var endPoint = CGPointMake(0.2, 1.3)
CGContextDrawLinearGradient(currentContext,gradient,startPoint,endPoint, 0)
CGContextRestoreGState(currentContext);
}
如果您跳轉到Xcode中的'CGGradientDrawingOptions'的定義(Cmd + click),你會發現它是一個枚舉值爲CGGradientDrawingOptions .DrawsBeforeStartLocation和CGGradientDrawingOptions .DrawsAfterEndLocation,並帶有來自Int32的初始化程序。 – Kevin