我正在使用以下内容创建在 IB 中可见的渐变视图:
import UIKit
import QuartzCore
@IBDesignable
class FAUGradientView: UIView {
@IBInspectable var firstColor:UIColor = UIColor.clear
@IBInspectable var secondColor:UIColor = UIColor.clear
@IBInspectable var startPoint:CGPoint = CGPoint(x: 0.0, y: 1.0)
@IBInspectable var endPoint:CGPoint = CGPoint(x: 1.0, y:0.0)
var gradientLayer:CAGradientLayer!
override func draw(_ rect: CGRect) {
super.draw(rect)
gradientLayer = CAGradientLayer()
self.gradientLayer.colors = [firstColor, secondColor]
self.gradientLayer.startPoint = self.startPoint
self.gradientLayer.endPoint = self.endPoint
self.gradientLayer.frame = self.frame
self.layer.addSublayer(self.gradientLayer)
}
}
但是,我在 IB 中得到的是纯黑色视图,而不是具有两种颜色渐变的视图,如下所示: