寫在前面
最近在學(xué)習(xí)swift,從github上下載很多demo進(jìn)行學(xué)習(xí),收獲不小,發(fā)現(xiàn)了一些不錯的寫法,記錄一下方便以后查詢,同時分享給大家,共同成長。
UI相關(guān)的一些常量和輔助方法
以下代碼主要定義了一個swift工程中的UI部分的常量亮和定義,當(dāng)然,這只是demo,正式工程可以按照這個思路進(jìn)行擴(kuò)展。
一個XYUI結(jié)構(gòu)體囊括了Screen
、Color
、Font
三個子結(jié)構(gòu)體,分別定義了屏幕、顏色、字體相關(guān)的常量和方法,結(jié)構(gòu)清晰,方便后續(xù)擴(kuò)展。
struct XYUI {
struct Screen {
static let Width : CGFloat = UIScreen.main.bounds.width
static let Height : CGFloat = UIScreen.main.bounds.size.height
static let NavH : CGFloat = XYUI.Screen.IphoneX == true ? 88 : 64
static let StatusbarH : CGFloat = XYUI.Screen.IphoneX == true ? 44 : 20
static let IphoneX: Bool = Int(XYUI.Screen.Height/XYUI.Screen.Width) == 216 //判斷是否是iPhoneX序列
}
// 顏色
struct Color {
/// 主色調(diào)
static let primary = UIColor.hexString(color: "#FFCA07")
static let black = UIColor.hexString(color: "#333333")
static let white = UIColor.white
}
struct Font {
static func fitFont(size:CGFloat) -> CGFloat {
if UIScreen.main.bounds.size.width == 320 {
return size * 0.8
}
return size
}
static let f10 = UIFont.systemFont(ofSize: 10)
static let f11 = UIFont.systemFont(ofSize: 11)
static let f12 = UIFont.systemFont(ofSize: 12)
static let f13 = UIFont.systemFont(ofSize: 13)
static let f14 = UIFont.systemFont(ofSize: 14)
static let f15 = UIFont.systemFont(ofSize: 15)
static let f16 = UIFont.systemFont(ofSize: 16)
static let f17 = UIFont.systemFont(ofSize: 17)
static let f18 = UIFont.systemFont(ofSize: 18)
static let f20 = UIFont.systemFont(ofSize: 20)
}
}
關(guān)于cellIdentifier使用
關(guān)于tableview和collectionView的cellIdentifier定義,在objective-c中,我之前是這樣定義的:
static const NSString *kXXXIdentifier = @"XXXIdentifier"; //XXX換成對應(yīng)cell的類名
后來發(fā)現(xiàn)了一個更簡便的寫法,就是在對應(yīng)的cell中,定義一個類方法,在類方法中使用反射機(jī)制進(jìn)行類名的獲取,從而生成復(fù)用標(biāo)識。代碼如下:
+ (NSString *)cellIdentifier{
return NSStringFromClass([self class]);
}
這樣就不用絞盡腦汁去想復(fù)用標(biāo)識的常量名了,而且更為簡潔。
在swift中通常的做法和在objective-c中一樣,定義一個常量,
static let kXXXIdentifier: String = "XXXIdentifier"; //XXX換成對應(yīng)cell的類名
更為簡潔的寫法:
public class func identifier() -> String {
let name: AnyClass! = object_getClass(self)
return NSStringFromClass(name)
}
從代碼上看,不管是objective-c還是swift,使用反射獲取的cellIdentifier和對應(yīng)的cell綁定在一起,不僅可以直接進(jìn)行代碼的copy復(fù)用,而且免去了在絞盡腦汁想復(fù)用標(biāo)識常量名的麻煩,何樂為不為呢。
根據(jù)對應(yīng)的屏幕尺寸進(jìn)行縮放
我們開發(fā)中進(jìn)行UI布局的時候,即使采用的是自動布局,一些控件的尺寸、控件間的間距也是應(yīng)該按照屏幕的大小進(jìn)行縮放的,這樣才能做到標(biāo)準(zhǔn)的UI自適應(yīng)。以下是以iPhone6、iPhone6s的屏幕尺寸為基準(zhǔn)進(jìn)行縮放的方法,特別收錄一下。
// 寬度比
let kWidthRatio = kScreenW / 375.0 //iPhone6、iPhone6s的寬度為基準(zhǔn)
// 高度比
let kHeightRatio = kScreenH / 667.0 //iPhone6、iPhone6s的高度為基準(zhǔn)
// 按比例自適應(yīng)
func Adapt(_ value : CGFloat) -> CGFloat {
return AdaptW(value)
}
// 自適應(yīng)寬度
func AdaptW(_ value : CGFloat) -> CGFloat {
return ceil(value) * kWidthRatio
}
// 自適應(yīng)高度
func AdaptH(_ value : CGFloat) -> CGFloat {
return ceil(value) * kHeightRatio
}
ceil函數(shù)從網(wǎng)上查了一下,意思是向上取整。
關(guān)于通知
iOS開發(fā)中,通知也是我們經(jīng)常使用的觀察者模式的一種實現(xiàn)。在OC中,我們通常把通知名定義成一個全局的常量,這樣方便調(diào)用和修改。
在OC中,我們之前是這樣做定義的:
NotificationGlobalName.h
extern NSString *const buildRouteNotification;
extern NSString *const placeDeletedNotification;
extern NSString *const centerPlaceOnMapNotification;
NotificationGlobalName.m
//`XXX`替換成工程名
NSString *const buildRouteNotification = @"XXX.buildRouteNotification";
NSString *const placeDeletedNotification = @"XXX.placeDeletedNotification";
NSString *const centerPlaceOnMapNotification = @"XXX.centerPlaceOnMapNotification";
我們可以在內(nèi)部使用#pragma mark -模塊名
分模塊定義,這樣方便后續(xù)的更新和查找。
swift中我們可以這樣做:
extension Notification.Name {
static let buildRoute = Notification.Name("buildRouteNotification")
static let placeDeleted = Notification.Name("placeDeletedNotification")
static let centerPlaceOnMap = Notification.Name("centerPlaceOnMapNotification")
}
利用擴(kuò)展把通知名定義成常量,方便后續(xù)的調(diào)用。