設定view漸變背景的工具方法

2021-07-08 18:27:21 字數 2241 閱讀 6270

避免設計美工切漸變背景圖,增加安裝包大小。雙色漸變的背景圖可以用**實現(多色漸變也可以實現,本工具類只抽取了雙色漸變,可以拓展)。廢話不多說,上**:

工具方法xdshadetool.h裡的方法宣告

/**

* 設定指定起止點的漸變背景

* imagesize 背景圖的size

* br bg bb 起始顏色rgb

* er eg bb 終止顏色rgb

* blocation 起始位置

* elocation 終止位置

*/+ (uiimageview *)setupshadeimgwithsize:(cgsize)imagesize begincolorr:(cgfloat)br bg:(cgfloat)bg bb:(cgfloat)bb endcolorr:(cgfloat)er eg:(cgfloat)eg eb:(cgfloat)eb beginlocation:(cgpoint)blocation endlocation:(cgpoint)elocation;

/** * 設定縱向漸變

* view 要漸變渲染的view

* begincolor 起始顏色

* endcolor 終止顏色

*/+ (void)setupendlongshadewithview:(uiview *)view begincolor:(uicolor *)begincolor endcolor:(uicolor *)endcolor;

工具方法xdshadetool.m裡的方法實現

//設定指定起止點的漸變背景

+ (uiimageview *)setupshadeimgwithsize:(cgsize)imagesize begincolorr:(cgfloat)br bg:(cgfloat)bg bb:(cgfloat)bb endcolorr:(cgfloat)er eg:(cgfloat)eg eb:(cgfloat)eb beginlocation:(cgpoint)blocation endlocation:(cgpoint)elocation

; cgfloat locations = ;

mygradient = cggradientcreatewithcolorcomponents(mycolorspace, components, locations, location_num);

cgcontextdrawlineargradient(context, mygradient, blocation,elocation,0); //兩個點,前乙個表示起點,第二個表示終點

uiimage *img = uigraphicsgetimagefromcurrentimagecontext();

uigraphicsendimagecontext();

uiimageview *imgview = [[uiimageview alloc] initwithimage:img];

return imgview;

}//設定縱向漸變

+ (void)setupendlongshadewithview:(uiview *)view begincolor:(uicolor *)begincolor endcolor:(uicolor *)endcolor

具體呼叫: 匯入標頭檔案#import 「xdshadetool.h」

//    自定義起始點的漸變背景

uiimageview *bg = [xdshadetool setupshadeimgwithsize:self.view

.bounds

.size begincolorr:17 bg:16 bb:200 endcolorr:99 eg:11 eb:30 beginlocation:cgpointmake(0, 0) endlocation:cgpointmake(self.view

.bounds

.size

.width, self.view

.bounds

.size

.height)];

[self.view addsubview:bg];

// 自定義顏色縱向漸變背景

[xdshadetool setupendlongshadewithview:self.view begincolor:[uicolor bluecolor] endcolor:[uicolor orangecolor]];

簡單好用,一直都選他

為UIView設定漸變背景

原文發布於 專案需要,需要在上顯示文字,但是文字的顏色很難控制,有時候與背景圖的顏色很接近導致文字難以看清楚,可以通過將上顯示文字的地方加一層黑色的半透明的背景色來解決這個問題。將這層背景色做成從黑色到透明的漸變。比如這樣一張圖,我需要在低端加上介紹文字 可以看到底下變得更黑了,文字更加清楚。實現方...

css設定背景色漸變

瀏覽器字首 background linear gradient red,blue background webkit linear gradient red,blue background o linear gradient red,blue background moz linear gradi...

VTK設定漸變背景色效果

最近想在自己的矩量法軟體中設定漸變背景色效果,查了vtk手冊及原始碼終於找到了方法,此處記錄一下,以防遺忘。vtk場景中的背景色可通過新增的renderer來 設定。vtk的繪製過程網上有一堆資料,此處只記錄背景色設定部分。renderer setbackground 1.0,1.0,1.0 設定頁...