UIView中的drawRect

想要重繪的話,僅僅創建一個UIView的父類並且重載drawRect

override func drawRect(regionThatNeedsToBeDrawn: CGRect)

你可以在外面畫一個需要重繪的區域,但是他不是最優化的。

永遠不要調用drawRect!!

反之,如果你得視圖需要重繪,通知系統

setNeedsDisplay()

setNeedsDisplayInRect(regionThatNeedsToBeRedrawn: CGRect

IOS之後會在恰當的時間調用deawRect


Core Graphics Concepts

你需要一個上下文
在drawRect裏 UIGraphicsGetCurrentContext() 給你一個可用的上下文創建路徑
設置屬性 like colors, fonts, textures, linewidths, linecaps, etc.
用給定的屬性填充上面創建的路徑。


Create a UIBezierPath,畫三角形

let path = UIBezierPath()

Move around, add lines or arcs to the path

path.moveToPoint(CGPoint(80, 50)) // assume screen is 160x250 

path.addLineToPoint(CGPoint(140, 150))

path.addLineToPoint(CGPoint(10, 150))

Close the path (if you want)

path.closePath()

Now that you have a path, set attributes and stroke/fill

// note this is a method in UIColor, not UIBezierPath

UIColor.greenColor().setFill()

// note this is a method in UIColor, not UIBezierPath

UIColor.redColor().setStroke()

// note this is a property in UIBezierPath, not UIColor

path.linewidth = 3.0

path.fill()

path.stroke()



//UIBezierPath畫圓

let roundRect = UIBezierPath(roundedRect: aCGRect, cornerRadius: aCGFloat)

//UIBezierPath畫橢圓

let oval = UIBezierPath(ovalInRect: aCGRect)

//畫圓角

addClip()






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章