兩段**,首先是使用方法
01
cgcontextref context = uigraphicsgetcurrentcontext();
02
nsarray *colors = [nsarray arraywithobjects:
03
[uicolor colorwithred:225.0 / 255.0 green:225.0 / 255.0 blue:225.0 / 255.0 alpha:1.0],
04
[uicolor colorwithred:168.0 / 255.0 green:168.0 / 255.0 blue:168.0 / 255.0 alpha:1.0],
05
nil];
06
[self _drawgradientcolor:context
07
rect:cgrectmake(rx, ry, rw, rh)
08
options:kcggradientdrawsafterendlocation
09
colors:colors];
10
cgcontextstrokepath(context);
// 描線,即繪製形狀
11
cgcontextfillpath(context);
// 填充形狀內的顏色
在一段就是繪製背景色漸變的矩形
01
/**
02
* 繪製背景色漸變的矩形,p_colors漸變顏色設定,集合中儲存uicolor物件(建立color時一定用三原色來建立)
03
**/
04
- (
void
)_drawgradientcolor:(cgcontextref)p_context
05
rect:(cgrect)p_cliprect
06
options:(cggradientdrawingoptions)p_options
07
colors:(nsarray *)p_colors
21
}
22
cggradientref gradient = cggradientcreatewithcolorcomponents(rgb, colorcomponents, null, colorcount);
23
cgcolorspacerelease(rgb);
24
cgpoint startpoint = p_cliprect.origin;
25
cgpoint endpoint = cgpointmake(cgrectgetminx(p_cliprect), cgrectgetmaxy(p_cliprect));
26
cgcontextdrawlineargradient(p_context, gradient, startpoint, endpoint, p_options);
27
cggradientrelease(gradient);
28
cgcontextrestoregstate(p_context);
// 恢復到之前的context
29
}
還有一種實現方式
1
cagradientlayer *gradient = [cagradientlayer layer];
2
gradient.frame = cgrectmake(rx, ry, rw, rh);
3
gradient.colors = [nsarray arraywithobjects:
4
(id)[uicolor blackcolor].cgcolor,
5
(id)[uicolor graycolor].cgcolor,
6
(id)[uicolor blackcolor].cgcolor,
7
nil];
8
[self.view.layer insertsublayer:gradient atindex:0];
iOS UIView背景色漸變
uiview theview uiview alloc initwithframe self headerbackimgview frame self headerview addsubview theview 初始化 cagradientlayer 物件,使它的大小為 uiview 的大小 cag...
DIV背景色漸變
ie下 filter alpha opacity 100 finishopacity 100 style 1,startx 0,starty 0,finishx 100,finishy 50 progid dximagetransform.microsoft.gradient startcolors...
背景色漸變處理 linear gradient
處理各個瀏覽器間的相容,背景色漸變 background moz linear gradient top,0192ed 0 0baeb3 100 background webkit gradient linear,left top,left bottom,color stop 0 0192ed co...