1. 程式人生 > >Swift學習仿照OC中的巨集定義

Swift學習仿照OC中的巨集定義

在Swift中不需要去建立header檔案,只需要建立一個類即可,什麼也不用配置。下面是一些本人總結的一些常用的巨集定義:

let GWIDTH  = UIScreen.main.bounds.size.width

let GHEIGHT = UIScreen.main.bounds.size.height

let isIPhoneX: Bool = GHEIGHT == 812 ? true : false

/// iPhone4裝置

let isIPhone4 = (max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.height) <

568.0 ? true : false)

/// iPhone5裝置

let isIPhone5 = (max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.height) == 568.0 ? true : false)

/// iPhone6裝置

let isIPhone6 = (max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.height) == 667.0 ? true : false)

/// iPhone6Plus裝置

let isIPhone6P = (max(UIScreen.main.

bounds.size.width, UIScreen.main.bounds.height) == 736.0 ? true : false)

let GetAppInfo = Bundle.main.infoDictionary//當前app資訊

let GetAppCurrentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")//獲取當前版本號

let GetSystemVersion = UIDevice.current.systemVersion//獲取裝置系統號

let isIphone =

UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone ? true : false//iphone裝置

let isIpad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad ? true : false//ipad裝置

//rgb

func COLORFROMRGB(r:CGFloat,_ g:CGFloat,_ b:CGFloat, alpha:CGFloat) -> UIColor

{

    return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: alpha)

}

//十六進位制顏色

func COLORFROM16(h:Int) ->UIColor {

    return COLORFROMRGB(r: CGFloat(((h)>>16) & 0xFF), CGFloat(((h)>>8) & 0xFF), CGFloat((h) & 0xFF), alpha: 1.0)

}