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()






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值