IOS 繪製背景色漸變的矩形

2021-06-20 01:32:39 字數 3113 閱讀 9026

兩段**,首先是使用方法

01cgcontextref context = uigraphicsgetcurrentcontext();

02nsarray *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],

05nil];

06[self _drawgradientcolor:context

07rect:cgrectmake(rx, ry, rw, rh)

08options:kcggradientdrawsafterendlocation

09colors:colors];

10cgcontextstrokepath(context);// 描線,即繪製形狀

11cgcontextfillpath(context);// 填充形狀內的顏色

在一段就是繪製背景色漸變的矩形

01/**

02* 繪製背景色漸變的矩形,p_colors漸變顏色設定,集合中儲存uicolor物件(建立color時一定用三原色來建立)

03**/

04- (void)_drawgradientcolor:(cgcontextref)p_context

05rect:(cgrect)p_cliprect

06options:(cggradientdrawingoptions)p_options

07colors:(nsarray *)p_colors

21}

22cggradientref gradient =  cggradientcreatewithcolorcomponents(rgb, colorcomponents, null, colorcount);

23cgcolorspacerelease(rgb);

24cgpoint startpoint = p_cliprect.origin;

25cgpoint endpoint = cgpointmake(cgrectgetminx(p_cliprect), cgrectgetmaxy(p_cliprect));

26cgcontextdrawlineargradient(p_context, gradient, startpoint, endpoint, p_options);

27cggradientrelease(gradient);

28cgcontextrestoregstate(p_context);// 恢復到之前的context

29}

還有一種實現方式

1cagradientlayer *gradient = [cagradientlayer layer];

2gradient.frame = cgrectmake(rx, ry, rw, rh);

3gradient.colors = [nsarray arraywithobjects:

4(id)[uicolor blackcolor].cgcolor,

5(id)[uicolor graycolor].cgcolor,

6(id)[uicolor blackcolor].cgcolor,

7nil];

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...