iOS常用的全局定义(swift版)
//获取屏幕宽度与高度
let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
let SCREENH_HEIGHT = UIScreen.mainScreen().bounds.size.height
//获取通知中心
let YYNotificationCenter = NSNotificationCenter.defaultCenter
//设置clear背景颜色
let YYClearColor = UIColor.clearColor()
//设置view的圆角和边框
func YYViewBorderRadius(view: UIView, radius: CGFloat, width: CGFloat, color: UIColor) {
view.layer.cornerRadius = radius
view.layer.masksToBounds = true
view.layer.borderWidth = width
view.layer.borderColor = color.CGColor
}
//获取view的frame
func YYGetViewWidth(view: UIView) -> CGFloat {
return view.frame.size.width
}
func YYGetViewHeight(view: UIView) -> CGFloat {
return view.frame.size.height
}
func YYGetViewX(view: UIView) -> CGFloat {
return view.frame.origin.x
}
func YYGetViewY(view: UIView) -> CGFloat {
return view.frame.origin.y
}
//获取图片资源
func YYGetImage(imageName: String) -> UIImage? {
return UIImage(named: imageName)
}
//获取当前语言
let YYCurrentLanguage = NSLocale.preferredLanguages().first!
//判断当前的设备
let IS_IPHONE: Bool = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone
let IS_IPAD: Bool = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad
let iPhone5SE: Bool = UIScreen.mainScreen().bounds.size.width == 320.0 && UIScreen.mainScreen().bounds.size.height == 568.0
let iPhone6_6s: Bool = UIScreen.mainScreen().bounds.size.width == 375.0 && UIScreen.mainScreen().bounds.size.height == 667.0
let iPhone6Plus_6sPlus: Bool = UIScreen.mainScreen().bounds.size.width == 414.0 && UIScreen.mainScreen().bounds.size.height == 736.0
//获取系统版本
let IOS_SYSTEM_VERSION: String = UIDevice.currentDevice().systemVersion
// dispath 工具方法
var GlobalMainQueue: dispatch_queue_t {
return dispatch_get_main_queue()
}
func delay(seconds seconds: Double, completion:()->()) {
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * seconds ))
dispatch_after(popTime, dispatch_get_main_queue()) {
completion()
}
}
//使用色值创建图片
func createImageWithColor(color: UIColor) -> UIImage { let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) UIGraphicsBeginImageContext(rect.size); let context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); let theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }