[英]UIView background color always BLACK while drawing UIBezierPath
I'm adding ShapeView which is subclass of UIView to View Controller in which I'm drawing Bezier Path and the background of that view always stays black.我将作为 UIView 的子类的 ShapeView 添加到 View Controller 中,我在其中绘制贝塞尔路径,并且该视图的背景始终保持黑色。 I tried to fix my problem with answers from UIView Background Color Always Black and Cant Change UIView Background Color From Black but, unfortunately, no results.我试图用UIView 背景颜色总是黑色并且无法更改 UIView 背景颜色从黑色的答案来解决我的问题,但不幸的是,没有结果。
The gradient green is shape, and the black areas under shape should be white.渐变绿色为形状,形状下的黑色区域应为白色。
Here is code from ShapeView class.这是来自 ShapeView class 的代码。
class ShapeView: UIView {
//// Color Declarations
// Green - Storage
let gradientColor0 = UIColor(red: 0.082, green: 0.608, blue: 0.486, alpha: 1.000)
let gradientColor1 = UIColor(red: 0.502, green: 0.980, blue: 0.949, alpha: 1.000)
override init(frame: CGRect) {
super.init(frame: frame)
self.isOpaque = false
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
//// General Declarations
let context = UIGraphicsGetCurrentContext()!
//// Gradient Declarations
let paint0_linear2 = CGGradient(colorsSpace: nil, colors: [gradientColor0.cgColor, gradientColor1.cgColor] as CFArray, locations: [0, 1])!
//// Bezier Drawing
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 342))
bezierPath.addLine(to: CGPoint(x: 187.5, y: 372))
bezierPath.addLine(to: CGPoint(x: 375, y: 342))
bezierPath.addLine(to: CGPoint(x: 375, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: 342))
bezierPath.close()
bezierPath.usesEvenOddFillRule = true
context.saveGState()
bezierPath.addClip()
context.drawLinearGradient(paint0_linear2,
start: CGPoint(x: 363.75, y: -664.71),
end: CGPoint(x: 900.13, y: 234.82),
options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
context.restoreGState()
}
}
Any ideas what to do?有什么想法该怎么做?
You may find this much easier to work with...你可能会发现这更容易使用......
@IBDesignable
class JurkoShapeView: UIView {
override open class var layerClass: AnyClass {
return CAGradientLayer.classForCoder()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
if let l = layer as? CAGradientLayer {
let gradientColor0 = UIColor(red: 0.082, green: 0.608, blue: 0.486, alpha: 1.000)
let gradientColor1 = UIColor(red: 0.502, green: 0.980, blue: 0.949, alpha: 1.000)
l.colors = [gradientColor0.cgColor, gradientColor1.cgColor]
l.startPoint = CGPoint(x: 0.0, y: 0.0)
l.endPoint = CGPoint(x: 0.0, y: 1.0)
}
}
override func layoutSubviews() {
super.layoutSubviews()
let l = CAShapeLayer()
let y1 = bounds.size.height - 30
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: bounds.maxX, y: 0))
bezierPath.addLine(to: CGPoint(x: bounds.maxX, y: y1))
bezierPath.addLine(to: CGPoint(x: bounds.midX, y: bounds.maxY))
bezierPath.addLine(to: CGPoint(x: 0, y: y1))
bezierPath.close()
l.path = bezierPath.cgPath
self.layer.mask = l
}
}
By making it @IBDesignable
you can see it while designing in Storyboard:通过将其@IBDesignable
,您可以在 Storyboard 中进行设计时看到它:
I think you can add those two lines to func draw(_ rect: CGRect)
just after super.draw(rect)
:我认为您可以在super.draw(rect)
之后将这两行添加到func draw(_ rect: CGRect)
) :
UIColor.white.setFill()
UIRectFill(rect)
so the method looks like this:所以方法看起来像这样:
override func draw(_ rect: CGRect) {
super.draw(rect)
UIColor.white.setFill()
UIRectFill(rect)
//// General Declarations
let context = UIGraphicsGetCurrentContext()!
//// Gradient Declarations
let paint0_linear2 = CGGradient(colorsSpace: nil, colors: [gradientColor0.cgColor, gradientColor1.cgColor] as CFArray, locations: [0, 1])!
//// Bezier Drawing
let bezierPath = UIBezierPath()
bezierPath.move(to: CGPoint(x: 0, y: 342))
bezierPath.addLine(to: CGPoint(x: 187.5, y: 372))
bezierPath.addLine(to: CGPoint(x: 375, y: 342))
bezierPath.addLine(to: CGPoint(x: 375, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: 0))
bezierPath.addLine(to: CGPoint(x: 0, y: 342))
bezierPath.close()
bezierPath.usesEvenOddFillRule = true
context.saveGState()
bezierPath.addClip()
context.drawLinearGradient(paint0_linear2,
start: CGPoint(x: 363.75, y: -664.71),
end: CGPoint(x: 900.13, y: 234.82),
options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
context.restoreGState()
}
If you need other background-colour you can change white
in: UIColor.white.setFill()
to needed colour.如果您需要其他背景颜色,您可以在UIColor.white.setFill()
white
更改为所需的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.