我的自定义形状后面的颜色已填充,我想将其设为白色
The Color behind my custom shape is filled and I want to make it white
基本上图片显示了它,但我想使橙色形状后面的灰色背景变得透明 and/or 白色(或坦率地说是任何颜色)。我只是想知道在哪里可以更改形状背景的颜色以及它的语法是什么。 IMAGE
class DemoView: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
var path: UIBezierPath!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.darkGray
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func createRectangle() {
path = UIBezierPath()
path.move(to: CGPoint(x:0.0, y: 0.0))
path.addLine(to: CGPoint(x: 0.0, y: 300.0))
//path.addLine(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
//self.frame.size.height
path.addCurve(to:CGPoint(x: self.frame.size.width, y: 300.0), controlPoint1: CGPoint(x: self.frame.size.width/2, y: 400), controlPoint2: CGPoint(x: self.frame.size.width/2, y: 400))
path.addLine(to: CGPoint(x: self.frame.size.width, y: 0.0))
path.close()
}
override func draw(_ rect: CGRect) {
self.createRectangle()
UIColor.orange.setFill()
path.fill()
UIColor.purple.setStroke()
path.stroke()
}
}
在下面的视图控制器中
let height: CGFloat = 400.0
let demoView = DemoView(frame: CGRect(x: 0,
y: self.view.frame.size.height/2 - height/2,
width: self.view.frame.size.width,
height: height))
self.view.addSubview(demoView)
您可以通过更改演示视图背景颜色来更改它
let demoView = DemoView(frame: CGRect(x: 0,
y: self.view.frame.size.height/2 - height/2,
width: self.view.frame.size.width,
height: height))
demoView.backgroundColor = .green
self.view.addSubview(demoView)
您也可以在 DemoView init
方法中更改它
class DemoView: UIView {
var path: UIBezierPath!
override init(frame: CGRect) {
super.init(frame: frame)
// self.backgroundColor = UIColor.darkGray
self.backgroundColor = // Any color you want or clear color
}
基本上图片显示了它,但我想使橙色形状后面的灰色背景变得透明 and/or 白色(或坦率地说是任何颜色)。我只是想知道在哪里可以更改形状背景的颜色以及它的语法是什么。 IMAGE
class DemoView: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
var path: UIBezierPath!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.darkGray
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func createRectangle() {
path = UIBezierPath()
path.move(to: CGPoint(x:0.0, y: 0.0))
path.addLine(to: CGPoint(x: 0.0, y: 300.0))
//path.addLine(to: CGPoint(x: self.frame.size.width, y: self.frame.size.height))
//self.frame.size.height
path.addCurve(to:CGPoint(x: self.frame.size.width, y: 300.0), controlPoint1: CGPoint(x: self.frame.size.width/2, y: 400), controlPoint2: CGPoint(x: self.frame.size.width/2, y: 400))
path.addLine(to: CGPoint(x: self.frame.size.width, y: 0.0))
path.close()
}
override func draw(_ rect: CGRect) {
self.createRectangle()
UIColor.orange.setFill()
path.fill()
UIColor.purple.setStroke()
path.stroke()
}
}
在下面的视图控制器中
let height: CGFloat = 400.0
let demoView = DemoView(frame: CGRect(x: 0,
y: self.view.frame.size.height/2 - height/2,
width: self.view.frame.size.width,
height: height))
self.view.addSubview(demoView)
您可以通过更改演示视图背景颜色来更改它
let demoView = DemoView(frame: CGRect(x: 0,
y: self.view.frame.size.height/2 - height/2,
width: self.view.frame.size.width,
height: height))
demoView.backgroundColor = .green
self.view.addSubview(demoView)
您也可以在 DemoView init
方法中更改它
class DemoView: UIView {
var path: UIBezierPath!
override init(frame: CGRect) {
super.init(frame: frame)
// self.backgroundColor = UIColor.darkGray
self.backgroundColor = // Any color you want or clear color
}