爲了節省任何人在這裏使用Google搜索的時間,我錯誤地認爲clearsContextBeforeDrawing
將也清除添加在drawRect中的任何圖層。它不是。要清除繪圖drawRect形狀圖層
當然,正如馬特所解釋的那樣,您當然只能在drawRect中繪製普通的方式,然後clearsContextBeforeDrawing
才能正常工作。例如,
override func drawRect(rect: CGRect)
{
// clearsContextBeforeDrawing works perfectly here as normal
// don't add any layers in here :)
let dotPath = UIBezierPath(ovalInRect:rect)
mainColor.setFill()
dotPath.fill()
let ins:CGFloat = (ringThickness/2)
let circlePath = UIBezierPath(ovalInRect: CGRectInset(rect,ins,ins))
circlePath.lineWidth = ringThickness
ringColor.setStroke()
circlePath.stroke()
}
這裏有一個好辦法在drawRect
override func drawRect(rect: CGRect)
{
let circlePath = UIBezierPath(ovalInRect:rect)
let circle = CAShapeLayer()
circle.path = circlePath.CGPath
circle.fillColor = someColor.CGColor
layer.addSublayer(circle)
... and a few more layers like ...
blah.fillColor = someColor.CGColor
layer.addSublayer(blah)
}
繪製但如何繪製之前清除?
設置clearsContextBeforeDrawing
什麼都不做。
我可以明確的唯一方法是通過刪除所有層:
override func drawRect(rect: CGRect)
{
// first remove all layers...
if let sublayers = layer.sublayers {
for layer in sublayers {
if layer.isKindOfClass(CAShapeLayer) //(essential)
{ layer.removeFromSuperlayer() }
}
}
...
}
有沒有更好的方式來清除「層」在畫什麼?
這是一個太開放的問題。我不知道你的總體目標是什麼。 – matt
無論如何,我完全明白 - 真棒。當然,我應該吸取當前的情況。然後'clearsContextBeforeDrawing'確實可行 - 好吧!所以我的困惑是,我認爲'clearsContextBeforeDrawing'也會「清除圖層」,如果你明白我的意思。你已經讓我平靜了:)尼斯... – Fattie