兩段代碼,首先是使用方法
創(chuàng)新互聯(lián)建站堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨?。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。十載網(wǎng)站建設(shè)經(jīng)驗(yàn)創(chuàng)新互聯(lián)建站是成都老牌網(wǎng)站營銷服務(wù)商,為您提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、H5場景定制、網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、小程序制作服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。
CGContextRef context = UIGraphicsGetCurrentContext(); NSArray *colors = [NSArray arrayWithObjects: [UIColor colorWithRed:225.0 / 255.0 green:225.0 / 255.0 blue:225.0 / 255.0 alpha:1.0], [UIColor colorWithRed:168.0 / 255.0 green:168.0 / 255.0 blue:168.0 / 255.0 alpha:1.0], nil]; [self _drawGradientColor:context rect:CGRectMake(rX, rY, rW, rH) options:kCGGradientDrawsAfterEndLocation colors:colors]; CGContextStrokePath(context);// 描線,即繪制形狀 CGContextFillPath(context);// 填充形狀內(nèi)的顏色
在一段就是繪制背景色漸變的矩形
/** * 繪制背景色漸變的矩形,p_colors漸變顏色設(shè)置,集合中存儲(chǔ)UIColor對(duì)象(創(chuàng)建Color時(shí)一定用三原色來創(chuàng)建) **/ - (void)_drawGradientColor:(CGContextRef)p_context rect:(CGRect)p_clipRect options:(CGGradientDrawingOptions)p_options colors:(NSArray *)p_colors { CGContextSaveGState(p_context);// 保持住現(xiàn)在的context CGContextClipToRect(p_context, p_clipRect);// 截取對(duì)應(yīng)的context int colorCount = p_colors.count; int numOfComponents = 4; CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); CGFloat colorComponents[colorCount * numOfComponents]; for (int i = 0; i < colorCount; i++) { UIColor *color = p_colors[i]; CGColorRef temcolorRef = color.CGColor; const CGFloat *components = CGColorGetComponents(temcolorRef); for (int j = 0; j < numOfComponents; ++j) { colorComponents[i * numOfComponents + j] = components[j]; } } CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colorComponents, NULL, colorCount); CGColorSpaceRelease(rgb); CGPoint startPoint = p_clipRect.origin; CGPoint endPoint = CGPointMake(CGRectGetMinX(p_clipRect), CGRectGetMaxY(p_clipRect)); CGContextDrawLinearGradient(p_context, gradient, startPoint, endPoint, p_options); CGGradientRelease(gradient); CGContextRestoreGState(p_context);// 恢復(fù)到之前的context }
還有一種實(shí)現(xiàn)方式
CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = CGRectMake(rX, rY, rW, rH); gradient.colors = [NSArray arrayWithObjects: (id)[UIColor blackColor].CGColor, (id)[UIColor grayColor].CGColor, (id)[UIColor blackColor].CGColor, nil]; [self.view.layer insertSublayer:gradient atIndex:0];
記得要
#import
此文的完成用到了四位老師的文章,在此表示感謝。
http://www.cnblogs.com/pengyingh/articles/2378840.html
http://www.dapps.net/dev/code/ios-dev-cgcontext-tips.html
http://www.cnblogs.com/smileEvday/archive/2012/06/05/UIColor_CIColor_CGColor.html
http://blog.csdn.net/reylen/article/details/8622932