Swift 常用功能方法封裝 1

2021-09-28 14:58:14 字數 3873 閱讀 5782

因為swift的函式在.swift檔案中是全域性可見的,所以只需要建立乙個工具檔案存放所有extension就很方便管理一些小函式和拓展。

1.獲取陣列的第二個節點或者第三個節點

extension

array

}var third:

element?}

}

2.在xib中設定tintcolor需要手動更新tintcolor才會顯示設定的值

extension

uiimageview

}

3.截圖螢幕(把任意view變成image)

extension

uiimage

}extension

uiview

}

4.簡化控制器跳轉

//返回上一層

extension

uiviewcontroller

else

}else

}func

pop()}

func

push

(_ viewcontroller:

uiviewcontroller?)

}func

pop(

_ completion:((

)->

void)?

)func

poproot

(_ completion:((

)->

void)?

)func

push

(_ viewcontroller:

uiviewcontroller?,

_ completion:((

)->

void)?

)}}

5.字典、字串互轉

//字典轉字串

extension

dictionary

}//字串轉字典

extension

string

return

nil}

func

isfloat()

->

bool

}

6.任意物件增加儲存屬性(乙個字典,用鍵值對形式儲存任何內容)(執行時新增屬性,原理參考runtime)

private

var key:

void

?extension

nsobject

get}

}

7.陰影

//陰影 shadow

extension

uiview

}

8.簡單的使用frame

//frame

extension

uiview

set}

var width:

cgfloat

set}

var height:

cgfloat

set}

var x:

cgfloat

set}

var centerx:

cgfloat

set}

var centery:

cgfloat

set}

var y:

cgfloat

set}

var maxx:

cgfloat

}var maxy:

cgfloat

}}

9.設定漸變色

//配置漸變色

extension

uiview}}

var locations =

[nsnumber](

)for index in1.

.. colors.

count

var cgcolors =

[cgcolor](

)for color in colors

let customlayer =

cagradientlayer()

customlayer.info =

["tag"

:nsnumber

.init

(value:

2983579)]

customlayer.colors = cgcolors

customlayer.locations = locations

customlayer.startpoint = from

customlayer.endpoint = to

customlayer.frame = frame

layer.

insertsublayer

(customlayer, at:0)

}//顏色和對應的位置,位置座標0-1,起點在左上角

func

configuregradient

(colors:

[uicolor

], from:

cgpoint

, to:

cgpoint

)}

10.十六進製制顏色生成

// 十六進製制顏色值轉換uicolor

func

hexcolor

(_ hex:

string)-

>

uicolor

if cstring.

hasprefix

("#"

)//字元chuan擷取

var range =

nsrange()

range.location =

0 range.length =

2let rstring =

(cstring as

nsstring).

substring

(with: range)

range.location =

2let gstring =

(cstring as

nsstring).

substring

(with: range)

range.location =

4let bstring =

(cstring as

nsstring).

substring

(with: range)

//儲存轉換後的數值

var r:

uint32=0

,g:uint32=0

,b:uint32=0

//進行轉換

scanner

(string: rstring)

.scanhexint32

(&r)

scanner

(string: gstring)

.scanhexint32

(&g)

scanner

(string: bstring)

.scanhexint32

(&b)

//根據顏色值建立uicolor

return

uicolor

(red:

cgfloat

(r)/

255.0

, green:

cgfloat

(g)/

255.0

, blue:

cgfloat

(b)/

255.0

, alpha:

1.0)

}

OEA體驗 常用功能1

一 摘要 寫的是我在使用oea中用到的功能,當然還有好多現在還沒有用到,希望高手們多多指點指點。oea 原始碼 oea框架 2.9 pre alpha 原始碼公布可以到bloodyangel 的部落格中可以下到。二 本文大綱a 摘要。b 資料過濾。c 託管屬性。d 支援樹型實體 二 資料過濾資料查詢...

OEA體驗 常用功能1

一 摘要 寫的是我在使用oea中用到的功能,當然還有好多現在還沒有用到,希望高手們多多指點指點。oea 原始碼 oea框架 2.9 pre alpha 原始碼公布可以到bloodyangel 的部落格中可以下到。二 本文大綱a 摘要。b 資料過濾。c 託管屬性。d 支援樹型實體 二 資料過濾資料查詢...

常用功能測試方法大全

超全常用功能測試方法 一 輸入框測試 1.字元型輸入框 1 字元型輸入框 英文全半形 數字 空或者空格 特殊字元 特別要注意單引號和 符號。禁止直接輸入特殊字元時,使用 貼上 拷貝 功能嘗試輸入。拷貝過去。3 空格檢查 輸入的字元間有空格 字元前有空格 字元後有空格 字元前後有空格 4 多行文字框輸...