【转】几点 iOS 开发技巧
[譯] 幾點 iOS 開發(fā)技巧
原文:iOS Programming Architecture and Design Guidelines
原文來自破船的分享
原文作者是開發(fā)界中知曉度相當(dāng)高的 Mugunth Kumar,他是 MKNetworkKit 的作者(雖然沒有 AFNetworking 使用那么廣泛,但也是一個很棒的 Network Kit),更是最近流傳甚廣的《iOS 5/6 Programming – Pushing The Limits》的作者。
文章中 MK 介紹了幾點開發(fā)中常用的小技巧,幾條 Tips 簡單易懂,但是很實用,不但可以提高開發(fā)效率,而且可以提高代碼的可讀性和可復(fù)用性。
Types in Objective-C 和 Naming Conventions 兩個章節(jié)介紹性內(nèi)容較多,下面從 Subclassing 開始簡單直譯一下,第一次翻譯,有諸多不到位的地方,各位多包涵。
Subclassing 繼承/子類
大多語言允許開發(fā)者子類化框架所提供的類,但是在 Objective-C 中不完全是這樣。大部分常用的類,諸如 NSArray、NSSet、NSDictionary 基本上都是集合類型的。不建議繼承這些類,除非你準(zhǔn)備轉(zhuǎn)發(fā)調(diào)用或者實現(xiàn)所有必要的原始方法。
在傳統(tǒng)的開發(fā)語言中,通常會通過繼承基礎(chǔ)類型(類似 NSArray 的類)來新增方法,重載已有的方法,或是自定義 UI 組件的外觀。在 Objective-C 中,一般通過 Category 來擴(kuò)展新方法。通過混合方法(swizzling the method?)來重載 SDK 提供的實現(xiàn)。以及外觀相關(guān)的代理協(xié)議(Protocol)來定制 UI 組件的外觀。
雖說如此,還是有一些類是經(jīng)常會繼承它們的,比如 UIViewController、UITableViewController、UIControl 等。繼承 UIViewController 大概是開發(fā)過程中最棒的一件事,因為它使得添加常見的功能變得異常簡單。在我開發(fā)的每個 App 中,會有一個繼承自 UIViewController 的子類,它實現(xiàn)了一組常用的方法。所有其他的 View Controllers 則都繼承自這個基礎(chǔ)類。
(譯者注:Web 開發(fā)中也常會有一個用于被繼承的 BaseController 來提供公共方法,看來開發(fā)是觸類旁通的,要多思考)
所以,以下繼承方法:
| 1 | @interface MyAppFeaturedYouTubeVideosViewController : UIViewController |
應(yīng)該替換成:
| 1 2 | @interface MyAppFeaturedYouTubeVideosFeaturedViewController : MyAppViewController @interface MyAppViewController : UIViewController |
這個公用基礎(chǔ)類可以在后續(xù)開發(fā)過程中用來添加公用的方法。在這個基礎(chǔ)父類中,我通常會申明以下方法:
| 1 2 3 4 5 6 | -(UIView*) errorView; -(UIView*) loadingView; -(void) showLoadingAnimated:(BOOL) animated; -(void) hideLoadingViewAnimated:(BOOL) animated; -(void) showErrorViewAnimated:(BOOL) animated; -(void) hideErrorViewAnimated:(BOOL) animated; |
實現(xiàn)如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | -(UIView*) errorView { return nil; } -(UIView*) loadingView { return nil; } -(void) showLoadingAnimated:(BOOL) animated { UIView *loadingView = [self loadingView]; loadingView.alpha = 0.0f; [self.view addSubview:loadingView]; [self.view bringSubviewToFront:loadingView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ loadingView.alpha = 1.0f; }]; } -(void) hideLoadingViewAnimated:(BOOL) animated { UIView *loadingView = [self loadingView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ loadingView.alpha = 0.0f; } completion:^(BOOL finished) { [loadingView removeFromSuperview]; }]; } -(void) showErrorViewAnimated:(BOOL) animated { UIView *errorView = [self errorView]; errorView.alpha = 0.0f; [self.view addSubview:errorView]; [self.view bringSubviewToFront:errorView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ errorView.alpha = 1.0f; }]; } -(void) hideErrorViewAnimated:(BOOL) animated { UIView *errorView = [self errorView]; double duration = animated ? 0.4f:0.0f; [UIView animateWithDuration:duration animations:^{ errorView.alpha = 0.0f; } completion:^(BOOL finished) { [errorView removeFromSuperview]; }]; } |
現(xiàn)在,App 中的每個 View Controller 中,可以很方便的通過調(diào)用以上方法來改變當(dāng)前 View 的狀態(tài)為 Loading 或者 Error。而且,View Controller 可以通過重載?-errorView?和?-loadingView?方法來提供自定義錯誤界面和 Loading 界面。
你還可以通過重載這個基礎(chǔ)類中的?-viewDidLoad?來統(tǒng)一修改所有 View 的表現(xiàn)。比如為所有的 View 添加相同的背景圖片或背景色:
| 1 2 3 4 5 | - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor appOffWhiteColor]; // changes all my views to "off-white" } |
UI Customization 自定義 UI
自定義 UI 可以大致分成兩類,一是自定義控件,二是皮膚/主題。前者可以讓 App 更出色,而后者是大部分 App 都需要的。我建議給 UIFont 和 UIColor 寫 Category 擴(kuò)展來提供自定義字體和自定義顏色。
例如,給 UIFont 添加如下方法:
| 1 2 3 4 5 6 7 8 9 | +(UIFont*) appFontOfSize:(CGFloat) pointSize { return [UIFont fontWithName:@"MyriadPro-Regular" size:pointSize]; } +(UIFont*) boldAppFontOfSize:(CGFloat) pointSize { return [UIFont fontWithName:@"MyriadPro-Black" size:pointSize]; } |
你就可以很方便地使用?[UIFont appFontOfSize:13]?得到?MyriadPro-Regular?字體。這樣當(dāng)你的設(shè)計需求變更時,就可以很快速的更換整個 App 中的字體。
相同的設(shè)計模式也可以應(yīng)用到自定義顏色中。給 UIColor 添加以下方法:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #define GREY(color) [UIColor colorWithRed:color/255.0 green:color/255.0 blue:color/255.0 alpha:1] +(UIColor*) appBackgroundColor { return [UIColor colorWithPatternImage:[UIImage imageNamed:@"BGPattern"]]; } +(UIColor*) appBlack1Color { return GREY(38); } +(UIColor*) appOffWhiteColor { return GREY(234); } |
所以,千萬不要用 Interface Builder 來選顏色。
Subclassing UILabels 繼承 UILabel
還有一個小竅門,當(dāng)開發(fā)者繼承 UILabel、UITextField 和 UITextView 時,通常在?-initWithFrame:?和?-initWithCoder:?方法中設(shè)置字體和顏色,參見以下代碼:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @implementation AppPrefixLabel -(void) setup { self.font = [UIFont fontWithName:@"SourceSansPro-Semibold" size:self.font.pointSize]; self.textColor = [UIColor redColor]; } -(id) initWithFrame:(CGRect)frame { if((self = [super initWithFrame:frame])) { [self setup]; } return self; } -(id) initWithCoder:(NSCoder *)aDecoder { if((self = [super initWithCoder:aDecoder])) { [self setup]; } return self; } @end |
這個技巧使得開發(fā)者可以在 Interface Builder 中自定義這些元素的外觀。在 IB 中拖入一個?UILabel,并且修改它的類為你自定義的類,瞬間就完成了這個 Label 字體和顏色的自定義,不用任何多余的代碼。
這個技巧多數(shù)情況下相當(dāng)管用,但是當(dāng)你的 App 支持自定義主題,且用戶可以通過設(shè)置界面更換主題時,就會顯得有些麻煩。
-initWithFrame:?和?initWithCoder:?會在 UI 組件創(chuàng)建的時候被調(diào)用,所以在這之后如果要改變字體和顏色,就需要很多額外的代碼。因此,如果你的 App 支持主題,寫一個主題管理器的全局單例來提供全局的主題、字體、顏色。
如果你用到了我說的第一個方法,你的?UIFont?的 Category 現(xiàn)在可以這樣實現(xiàn)了:
| 1 2 3 4 5 | +(UIFont*) appFontOfSize:(CGFloat) pointSize { NSString *currentFontName = [[ThemeProvider sharedInstance] currentFontName]; return [UIFont fontWithName:currentFontName size:pointSize]; } |
UIColor 同理。其實沒有正確或錯誤的方法,上述方法都是可行的。
遵從這里提到的設(shè)計模式,可以讓你的代碼干凈得像寫的很漂亮的 JS/CSS。試著在你的下一個項目中用這些方法吧。
Allen 后記
之前在想 iOS 開發(fā)到底是否需要一個類似 Web 開發(fā)中的所謂的框架,但漸漸發(fā)現(xiàn)其實 iOS SDK 本就是一個高度封裝了的框架了,可能我們需要的不是更更高層的框架,而是一種好的設(shè)計模式、開發(fā)習(xí)慣和代碼結(jié)構(gòu)。因此是不是可以從一個 Project 的層面出發(fā),寫一個干凈的框架,并且定義一些規(guī)范,就是一個很好的“框架”了?而不是非得提供 Router 之類的往 Web 開發(fā)框架去靠。
轉(zhuǎn)載于:https://www.cnblogs.com/ymonke/p/3481469.html
總結(jié)
以上是生活随笔為你收集整理的【转】几点 iOS 开发技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何处理xml文件!看看这里
- 下一篇: VS2010程序打包操作