0
我想为每个UIBezierPath部分设置不同的笔触颜色。但顺序是完全错误的,我不知道如何解决它。按UIGraphicsGetCurrentContext设置不同的setStroke颜色
这就是我想要的东西:
而这就是我得到:
好像该命令是错误的。有没有办法将颜色“绑定”到bezierPath并将其附加到上下文中?我的代码如下。谢谢!
let size = CGSize(width: 134, height:51)
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
//// Rectangle Drawing
let rectanglePath = UIBezierPath(roundedRect: CGRect(x: 0.5, y: 0.5, width: 126, height: 50), cornerRadius: 3)
UIColor.lightGray.setStroke()
rectanglePath.lineWidth = 1
rectanglePath.stroke()
let clipPath: CGPath = rectanglePath.cgPath
context?.addPath(clipPath)
//// Rectangle 2 Drawing
let rectangle2Path = UIBezierPath(roundedRect: CGRect(x: 3, y: 3, width: 121, height: 45), cornerRadius: 3)
UIColor.green.setFill()
rectangle2Path.fill()
let clipPathh: CGPath = rectangle2Path.cgPath
context?.addPath(clipPathh)
let rectangle3Path = UIBezierPath(roundedRect: CGRect(x: 128, y: 18, width: 6, height: 14), byRoundingCorners: [.topRight, .bottomRight], cornerRadii: CGSize(width: 3, height: 3))
UIColor.gray.setFill()
rectangle3Path.fill()
let clipPathhh: CGPath = rectangle3Path.cgPath
context?.addPath(clipPathhh)
context?.closePath()
// Convert to UIImage
let cgimage = context!.makeImage();
let uiimage = UIImage(cgImage: cgimage!)
// End the graphics context
UIGraphicsEndImageContext()
image.image = uiimage;
我用UIImageView通过IB引入了一个相当粗糙的代码拷贝/粘贴到一个新的项目中。我得到了正确的结果。绿色填充周围的浅灰色边框。不知道为什么它适合我。现在,如果你真正想要的是在白色边框之外有绿色填充的灰色边框,则可能需要第三条白色路径。 – dfd
Mhh,你把它粘贴到viewDidLoad中了吗? – da1lbi3
是的。再次,粗略快速复制/过去。对于贝塞尔路径,你通常希望视图的drawRect中有东西。 – dfd