2016-11-19 72 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來提供整個路徑。