2016-11-19 71 views
0

我想为每个UIBezierPath部分设置不同的笔触颜色。但顺序是完全错误的,我不知道如何解决它。按UIGraphicsGetCurrentContext设置不同的setStroke颜色

这就是我想要的东西:

enter image description here

而这就是我得到:

enter image description here

好像该命令是错误的。有没有办法将颜色“绑定”到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; 
+0

我用UIImageView通过IB引入了一个相当粗糙的代码拷贝/粘贴到一个新的项目中。我得到了正确的结果。绿色填充周围的浅灰色边框。不知道为什么它适合我。现在,如果你真正想要的是在白色边框之外有绿色填充的灰色边框,则可能需要第三条白色路径。 – dfd

+0

Mhh,你把它粘贴到viewDidLoad中了吗? – da1lbi3

+0

是的。再次,粗略快速复制/过去。对于贝塞尔路径,你通常希望视图的drawRect中有东西。 – dfd

回答

1

明白了。过了一段时间,我与贝塞尔路径一起工作,但有点玩弄发现问题 - 这一切都在顺序。该代码应该是:

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 
let clipPath: CGPath = rectanglePath.cgPath 
context?.addPath(clipPath) 
rectanglePath.stroke() 

//// Rectangle 2 Drawing 
let rectangle2Path = UIBezierPath(roundedRect: CGRect(x: 3, y: 3, width: 121, height: 45), cornerRadius: 3) 

UIColor.green.setFill() 
let clipPathh: CGPath = rectangle2Path.cgPath 
context?.addPath(clipPathh) 
rectangle2Path.fill() 

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() 
let clipPathhh: CGPath = rectangle3Path.cgPath 
context?.addPath(clipPathhh) 
rectangle3Path.fill() 

// Convert to UIImage 
let cgimage = context!.makeImage(); 
let uiimage = UIImage(cgImage: cgimage!) 

// End the graphics context 
UIGraphicsEndImageContext() 


imageView.image = uiimage; 

注意,你不需要填充/行程添加路径上下文之后。另请注意,closePath调用没有影响,因为您已通过定义rects来提供整个路径。