UIView中的drawRect

来源:互联网 发布:v meca mt2软件 编辑:程序博客网 时间:2024/06/10 09:51

想要重绘的话,仅仅创建一个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()






0 0