如何更改 UIBezierPath 背景颜色?
How to change UIBezierPath background color?
我添加了这些代码行:
var radius: CGFloat = UIScreen.mainScreen().bounds.width - 60
let path: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height), cornerRadius: 0)
let circlePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 30, y: (UIScreen.mainScreen().bounds.height - radius) / 2 - (navigationController?.navigationBar.frame.height)!, width: radius, height: radius), cornerRadius: radius)
path.appendPath(circlePath)
但它给了我这个结果
如何让我的圆圈背景颜色 - clearColor()?
我试过这个:
UIColor.whiteColor().setFill()
circlePath.fill()
但是没有任何意义
试试这个:
let context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor);
试试这个:
let radius: CGFloat = 50
let path: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 50, height: 50), cornerRadius: 0)
let circlePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: radius, height: radius), cornerRadius: radius)
path.append(circlePath)
let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath
fillLayer.fillColor = UIColor.red.cgColor
yourView.layer.addSublayer(fillLayer)
路径没有颜色;图层有颜色。
您需要根据路径创建图层,并将其添加到您的视图图层,如上面的代码所示。
我添加了这些代码行:
var radius: CGFloat = UIScreen.mainScreen().bounds.width - 60
let path: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height), cornerRadius: 0)
let circlePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 30, y: (UIScreen.mainScreen().bounds.height - radius) / 2 - (navigationController?.navigationBar.frame.height)!, width: radius, height: radius), cornerRadius: radius)
path.appendPath(circlePath)
但它给了我这个结果
如何让我的圆圈背景颜色 - clearColor()?
我试过这个:
UIColor.whiteColor().setFill()
circlePath.fill()
但是没有任何意义
试试这个:
let context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor);
试试这个:
let radius: CGFloat = 50
let path: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 50, height: 50), cornerRadius: 0)
let circlePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: radius, height: radius), cornerRadius: radius)
path.append(circlePath)
let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath
fillLayer.fillColor = UIColor.red.cgColor
yourView.layer.addSublayer(fillLayer)
路径没有颜色;图层有颜色。
您需要根据路径创建图层,并将其添加到您的视图图层,如上面的代码所示。