drawRect:一点绘图知识总结

本文介绍了iOS中自定义视图绘图的核心知识,重点讲解了drawRect:方法的工作原理和CGContextRef绘图上下文。通过设置线宽、颜色、路径,使用CGContextStrokePath(context)进行描边。同时,文章探讨了CGContextStrokePath()函数的细节,指出其在完成绘制后会清除当前路径,导致无法叠加绘制效果的问题。
摘要由CSDN通过智能技术生成

1.当一个类是UIView或UIView的子类时,这个类会调用drawRect:用来绘制显示在手机屏幕的图片,通常我们把自定义的绘图时,会覆盖此方法并把代码写在这个方法里,每当UIView的实例要绘图或重绘时,系统会为这个View准备一个绘图上下文,然后这个上下文会被激活,drawRect:消息会被发送。绘图上下文的类型是CGContextRef负责合成绘图命令和生成图片,保存绘图状态。当我们调用UIKit框架中的类和方法,这是会隐式地使用当前激活的绘图上下文。在其他情况下你需要取得绘图上下文,然后调用Core Graphics框架的C函数来绘图。(Each time an instance of UIView needs to be drawn (or redrawn), the system prepares a graphics context specifically for that view. Then the context is activated, and the message drawRect: is sent to the instance of UIView that is being drawn. The graphics context’s type is CGContextRef (Core
Graphics Context Reference), and it is responsible for aggregating drawing commands and producing an image as a result. This image is the appearance of the view instance. A graphics context also stores its drawing state, which includes things like the current drawing color, coordinate system, and the
current line width.)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值