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