UIKit类下的所有东西都不是线形安全的,需要放在主线程中。 The View Drawing Cycle UIKit主要涉及的是更新UIView对象的内容,采用UIView自带的代码drawRect:。需要注意的是,该函数仅仅在UIView第一次被加载的时候调用一次,用于画该view中所有的可见部分。在其它时候,仅仅是画view中需要更新的那部分。可以触发redraw的情况如下: 1、移动或者去除当前部分覆盖在该view上的其它view时 2、将之前的一个隐藏的view显现出来 3、把一个view从本view上scroll来回时 4、调用setNeedsDisplay和setNeedsDisplayInRect:两个方法时 Coordinates and Coordinate Transforms UIKit的view的坐标原点位于view的左上角,坐标轴分别向下和向右延伸。CTM{currrent transformation matrix}便是一个将当前设备屏幕的坐标系和画出图形的坐标系相关联的一个东西。当view的drawRect:函数被调用的时候,CTM便被设置为将两者统一。但是可以通过修改CTM的scaling,ration,translation因子来改变图形的大小,方向,位置等。 比如在(20,20)处一个图形,想让他移动到(10,10),通过CTM可以很容易实现。如果当初图形是通过path实现的,那么这么一个位置的改变,将是非常麻烦的。两种方法修改CTM,一种是通过CGContext类中的方法,另一种是通过CGAffineTransform。具体可以参考 Quartz 2D Programming Guide and CGAffineTransform Reference. Graphics Context 在调用drawRect:函数进行画图之前,需要进行一些配置。最开始,UIView的对象需要创建一个context,它是作为当前真正的作图环境的。设置context包括设置颜色,clipping area(这个可以参考developer网站中的mask部分),线寛,字体信息等。 你可以随意的创建自己的context,而不一定是当前view的。通过这个你可以用于捕捉玩家的一些操作或者是根据操作创建一个图片或者pdf文档。具体可以参考:CGBitmapContextCreate or CGPDFContextCreate 。 Default Coordinate Systems and Drawing in iOS 在iOS中,不同的framework可以有不同的坐标系,这一点需要注意。 1、ULO(upper-left-origin),左上角 如 UIKit和Core Animation框架 2、LLO(lower-left-origin),左下角 如 Core Graphics框架 Drawing to the Screen UIKit采用的是ULO,而Core Graphics采用的是LLO,如果希望用Core Graphics的函数在View中画图的话,必须采用ULO,另外两个框架的函数区分可以通过函数的开头字母了解,比如UIGraphicsGetCurrentContext()和CGContextRef。 针对UIGraphicsContext函数的返回值,在pdf文档中返回一个pdf context,在图片的文档中返回一个图片的context。 Drawing to Bitmap Context and PDF Context
Side Effects of Drawing with Different Coordinate Systems
此部分略过········· 下面开始新的内容。 Color 和 Color Spaces
The UIColor object provides convenience methods for specifying color values using RGB, HSB, and grayscale values. When creating colors in this way, you never need to specify the color space. It is determined for you automatically by the UIColor object.
You can also use the CGContextSetRGBStrokeColor and CGContextSetRGBFillColor functions in the Core Graphics framework to create and set colors. Although the Core Graphics framework includes support for creating colors using other color spaces, and for creating custom color spaces, using those colors in your drawing code is not recommended. Your drawing code should always use RGB colors.
这部分就不翻译了,非常简单易懂。
Improving Drawing Performance 画画面在任何的平台上都是非常昂贵的,合理的组织自己的代码是十分重要的。下面是一些建议: 1、Drawing minimally 在每一个循环周期中,不要试图更新整个画图区域,尽量只更新实际变化了的区域。例如:如果你采用UIView的drawRect:函数进行画图,那么一定要传递正确的参数,用以显示更新的范围。 2、Mark opaque views as such 构造一个不透明的内容比构造半透明的开销少很多。通过让view的content不包含任何的透明元素以及设置view.opaque = YES 3、Resuse table cells and views during scrolling 在滑动的过程中创建view的开销非常大,耗时非常长,这部分耗时,可以导致更新屏幕的时间减少,从而导致了不平衡的滑动,给人卡的感觉 4、Avoid clearing the previous content during scrolling 默认情况下,UIKit首先会情况context的buffer,之后才会调用drawRect:函数。在滑动的时候,多次清空buffer开销很大。这一点可以通过设置view.clearsContextBeforeDrawing = NO 5、Minimize graphics state changes while drawing 在view中修改图片的状态也是开销很大的操作。如果你需要画一些状态信息相同的东西,那么试图把他们放在一起画,可以减少需要修改的状态。 Drawing With Quartz and UIKit Quartz是iOS中native window server和drawing technology的general name。Core Graphics框架是Quartz的核心。该框架提供了一系列的函数方便了绘图。具体可以参考: UIKit Framework Reference. Configuring The Graphics Context 绘图肯定是需要绘在context中的,在调用drawRect:的时候,系统已经创建了对应的CGContextRef类型的数据,可以通过UIGraphicsGetCurrentContext:函数获得此对象。参考: CGContext Reference
Table 1-2Core graphics functions for modifying graphics state