随机生成的 CGGradient 不会显示
Randomly generated CGGradient wont display
我正在编写一个生成随机渐变并使用自定义 UIView
Class 在屏幕上显示它们的应用。因为我对app规划的功能比较多,所以我选择了自己给渐变class。生成渐变的代码在这里:
import UIKit
class Gradient {
var topColor = UIColor()
var bottomColor = UIColor()
var gradient: CGGradientRef?
func generateGradient() -> CGGradientRef {
var red = CGFloat(arc4random_uniform(256))
var green = CGFloat(arc4random_uniform(256))
var blue = CGFloat(arc4random_uniform(256))
topColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
red = CGFloat(arc4random_uniform(256))
green = CGFloat(arc4random_uniform(256))
blue = CGFloat(arc4random_uniform(256))
bottomColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
let gradientColors = [topColor.CGColor, bottomColor.CGColor]
let gradientStops:[CGFloat] = [0, 1.0]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let generatedGradient = CGGradientCreateWithColors(colorSpace, gradientColors, gradientStops)
return generatedGradient
}
init() {
gradient = self.generateGradient()
}
}
显示渐变的渐变查看器class显示为:
import UIKit
@IBDesignable class Gradient_Viewer: UIView {
override func drawRect(rect: CGRect) {
let background = Gradient()
let context = UIGraphicsGetCurrentContext()
let startPoint = CGPoint.zeroPoint
let endPoint = CGPoint(x: 0, y: self.bounds.height)
CGContextDrawLinearGradient(context, background.gradient, startPoint, endPoint, 0)
}
}
渐变生成有问题,还是我没有正确访问渐变?
@IBDesignable class Gradient_Viewer: UIView {
var random8bit: CGFloat {
return CGFloat(arc4random_uniform(256))/255.0
}
var gradient: CGGradientRef {
return CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [UIColor(red: random8bit, green: random8bit, blue: random8bit, alpha: 1.0).CGColor, UIColor(red: random8bit, green: random8bit, blue: random8bit, alpha: 1.0).CGColor], [0, 1.0])
}
override func drawRect(rect: CGRect) {
CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, CGPoint.zeroPoint, CGPoint(x: 0, y: self.bounds.height), 0)
}
}
我正在编写一个生成随机渐变并使用自定义 UIView
Class 在屏幕上显示它们的应用。因为我对app规划的功能比较多,所以我选择了自己给渐变class。生成渐变的代码在这里:
import UIKit
class Gradient {
var topColor = UIColor()
var bottomColor = UIColor()
var gradient: CGGradientRef?
func generateGradient() -> CGGradientRef {
var red = CGFloat(arc4random_uniform(256))
var green = CGFloat(arc4random_uniform(256))
var blue = CGFloat(arc4random_uniform(256))
topColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
red = CGFloat(arc4random_uniform(256))
green = CGFloat(arc4random_uniform(256))
blue = CGFloat(arc4random_uniform(256))
bottomColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
let gradientColors = [topColor.CGColor, bottomColor.CGColor]
let gradientStops:[CGFloat] = [0, 1.0]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let generatedGradient = CGGradientCreateWithColors(colorSpace, gradientColors, gradientStops)
return generatedGradient
}
init() {
gradient = self.generateGradient()
}
}
显示渐变的渐变查看器class显示为:
import UIKit
@IBDesignable class Gradient_Viewer: UIView {
override func drawRect(rect: CGRect) {
let background = Gradient()
let context = UIGraphicsGetCurrentContext()
let startPoint = CGPoint.zeroPoint
let endPoint = CGPoint(x: 0, y: self.bounds.height)
CGContextDrawLinearGradient(context, background.gradient, startPoint, endPoint, 0)
}
}
渐变生成有问题,还是我没有正确访问渐变?
@IBDesignable class Gradient_Viewer: UIView {
var random8bit: CGFloat {
return CGFloat(arc4random_uniform(256))/255.0
}
var gradient: CGGradientRef {
return CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [UIColor(red: random8bit, green: random8bit, blue: random8bit, alpha: 1.0).CGColor, UIColor(red: random8bit, green: random8bit, blue: random8bit, alpha: 1.0).CGColor], [0, 1.0])
}
override func drawRect(rect: CGRect) {
CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, CGPoint.zeroPoint, CGPoint(x: 0, y: self.bounds.height), 0)
}
}