绘图 - 6

来源:互联网 发布:马布里nba数据 编辑:程序博客网 时间:2024/06/04 19:37

上一篇章用了一个例子,并没有分析这个例子的作用:

- (void)drawRect:(CGRect)rect{    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSaveGState(context);        size_t gradLocationsNum = 2;    CGFloat gradLocations[2] = {0.0f, 1.0f};    CGFloat gradColors[8] = {0, 0, 0, 0.3, 0, 0, 0, 0.8};        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);    CGColorSpaceRelease(colorSpace), colorSpace = NULL;        CGPoint gradCenter= CGPointMake(round(CGRectGetMidX(self.bounds)), round(CGRectGetMidY(self.bounds)));    CGFloat gradRadius = MAX(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));    CGContextDrawRadialGradient(context, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);    CGGradientRelease(gradient), gradient = NULL;        CGContextRestoreGState(context);}

效果:


非常适合做遮挡层的dim。阴暗色,当然你需要什么颜色google即可。


首先的:

/* Push a copy of the current graphics state onto the graphics state stack.   Note that the path is not considered part of the graphics state, and is   not saved. */CG_EXTERN void CGContextSaveGState(CGContextRef __nullable c)    CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

CGContextSaveGState 直面意义的保存上下文的状态,是通过将当前的图形状态压入图形状态栈。


紧接着这个方法的有另外一个形式上相似的方法:

/* Restore the current graphics state from the one on the top of the   graphics state stack, popping the graphics state stack in the process. */CG_EXTERN void CGContextRestoreGState(CGContextRef __nullable c)    CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

这个理所当然是将栈中的存储的状态弹出。

而这两个通常一对使用。


具体参考:

http://wsqwsq000.iteye.com/blog/1316277


说到这两个就不得不提另外两个非常相似的方法:

UIKIT_EXTERN void UIGraphicsPushContext(CGContextRef context);UIKIT_EXTERN void UIGraphicsPopContext(void);

看前缀就知道是另外的库的,这里还是将重心留到CG的方面。

http://blog.csdn.net/lihangqw/article/details/9969961


typedef struct CGColorSpace *CGColorSpaceRef;

另外还有这个封装了颜色信息的结构体。

google一下就可以知道是关于渐变色的信息:

http://www.jianshu.com/p/f80ef6219d6b

http://www.cnblogs.com/pengyingh/articles/2378840.html



有几种创建的方式,其中RGB方式:

</pre><pre name="code" class="objc">CG_EXTERN CGColorSpaceRef __nullable CGColorSpaceCreateDeviceRGB(void)

同理:

typedef struct CGGradient *CGGradientRef;

这都是关于色彩的,用的时候查即可。


上面简单地介绍了一下几个绘图用到的类、结构体、方法,2D绘图库提供的方法远远不止那么少,请大家多多查阅!


0 0