今天我們來談?wù)勅绾未罱蚣埽蚣苄枰鲆恍┦裁础?/p>
第一步:找到我們的目標(biāo)
我們的目標(biāo)是讓其他開發(fā)人員拿到手后即可寫頁面,不再需要考慮其他的問題力惯。
第二步:我們需要做哪些東西
各位跟著我一步一步來進(jìn)行放棒。
假定我們即將要寫的項(xiàng)目是一個(gè)tabbar+navgation的項(xiàng)目結(jié)構(gòu)。
1. 新建工程
1.1 刪除不必要類目
選擇Single ViewApplication站欺,命名為HomeHome
刪除選中的三項(xiàng)。
1.2 修改info.plist
1.刪除info.plist main.storyboard字段
2.添加字段
info.plist 中添加以下
Bundle display name --- 對應(yīng)應(yīng)用名
開啟http訪問纤垂,不添加該字段導(dǎo)致不能訪問http镊绪,只能訪問https。
3.添加應(yīng)用白名單
iOS9之后分享等必須添加白名單后才可以打開相關(guān)應(yīng)用洒忧。字段值為LSApplicationQueriesSchemes
-
添加URL Types
用于分享到QQ蝴韭,微信等平臺或者打開支付寶等,已經(jīng)設(shè)置當(dāng)前APP的Url shesmes
1.3 修改項(xiàng)目配置
-
關(guān)閉bitCode
build setting中搜索bitCode 改為NO
2.配置項(xiàng)目
2.1 建立目錄
2.2 拖入各大依賴庫
我在這里主要依賴了以下庫:
RDVTabBarController 一個(gè)很好用的tabbar控件
2.3 依賴常用第三方服務(wù)
通常集成了:
均根據(jù)官方文檔安裝
3. 編寫代碼
3.1 建立pch
3.1 建立Api.h文件
該聲明文件用于查詢接口等
3.2 建立Config.h文件
該聲明文件用于編寫某些全局配置參數(shù)
如以下:
#define kPushPhotoBrowserNotifitationName @"PushPhotoBrowser"
#define kPresentVideoPlayerNotifitationName @"playCallBackVideo"
#define APPICONIMAGE [UIImage imageNamed:[[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject]]
#define APPNAME [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]
#define Main_Color [UIColor colorWithRed:(3)/255.0 green:(160)/255.0 blue:(235)/255.0 alpha:1.0]
#define Main2_Color [UIColor colorWithRed:(135)/255.0 green:(202)/255.0 blue:(231)/255.0 alpha:1.0]
#define VTColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define Text_Color [UIColor colorWithRed:(51)/255.0 green:(71)/255.0 blue:(113)/255.0 alpha:1.0]
#define BackGround_Color [UIColor colorWithRed:(235)/255.0 green:(235)/255.0 blue:(241)/255.0 alpha:1.0]
#define Default_Person_Image [UIImage imageNamed:@"default_parents"]
#define Default_General_Image [UIImage imageNamed:@"default_general"]
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
//以及各種第三方服務(wù)商的appId或者App key
3.3 開始編寫項(xiàng)目結(jié)構(gòu)
我們分別建立了3個(gè)AppDelegate的類別
HHAppDelegate+AppService //app的服務(wù)管理
HHAppDelegate+AppLifeCircle//app的生命周期管理
HHAppDelegate+RootController//app的跟視圖控制器實(shí)例
先看看HHAppDelegate+RootController
聲明文件
/**
* 首次啟動(dòng)輪播圖
*/
- (void)createLoadingScrollView;
/**
* tabbar實(shí)例
*/
- (void)setTabbarController;
/**
* window實(shí)例
*/
- (void)setAppWindows;
/**
* 設(shè)置根視圖
*/
- (void)setRootViewController;
實(shí)現(xiàn):
- (void)setRoot
{
UINavigationController * navc = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navc.navigationBar.barTintColor = Main_Color;
navc.navigationBar.shadowImage = [[UIImage alloc] init];
[navc.navigationBar setTranslucent:NO];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[navc.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
navc.navigationBar.tintColor = [UIColor whiteColor];
self.window.rootViewController = navc;
}
- (void)setTabbarController
{
HomePageViewController *school = [[HomePageViewController alloc]init];
AboutChildViewController *child = [[AboutChildViewController alloc]init];
CommuntiyViewController *edu = [[CommuntiyViewController alloc]init];
SZCourseListViewController *courseList = [[SZCourseListViewController alloc]init];
AboutMeViewController *about = [[AboutMeViewController alloc]init];
RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
[tabBarController setViewControllers:@[school,edu,child,courseList,about]];
self.viewController = tabBarController;
tabBarController.delegate = self;
[self customizeTabBarForController:tabBarController];
}
- (void)setAppWindows
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication]setStatusBarHidden:NO];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
- (void)createLoadingScrollView
{
//引導(dǎo)頁實(shí)例
}
注意:這里將Navgation作為根視圖控制器熙侍,tabbar作為Navgation的rootViewController榄鉴。
如此全局只有一個(gè)跟導(dǎo)航控制器。頁面Controller與Tabbar處于平級狀態(tài)蛉抓。這與接下來我們彈出各種頁面均由關(guān)聯(lián)
自此一個(gè)大致的結(jié)構(gòu)就出來了庆尘,一個(gè)Tabbar+Navgation的格式
3.4 編寫AppService
這類信息大都跟需求有關(guān),我們在這里處理項(xiàng)目相關(guān)信息巷送。如可能會由以下信息
- (void)registerBugly;
/**
* 基本配置
*/
- (void)configurationLaunchUserOption;
/**
* 友盟注冊
*/
- (void)registerUmeng;
/**
* Mob注冊
*/
- (void)registerMob;
/**
* 檢查更新
*/
- (void)checkAppUpDataWithshowOption:(BOOL)showOption;
/**
* 上傳用戶設(shè)備信息
*/
- (void)upLoadMessageAboutUser;
/**
* 檢查黑名單用戶
*/
-(void)checkBlack;
3.5 App結(jié)構(gòu)整理
以上我們處理了跟視圖驶忌,服務(wù),app聲明周期等方法實(shí)現(xiàn)
現(xiàn)在我們開始調(diào)用這些方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setAppWindows];
[self setTabbarController];
[self setRootViewController];
[self configurationLaunchUserOption];
[self registerBugly];
[self registerMob];
[self registerUmeng];
[VTJpushTools setupWithOptions:launchOptions];
[self upLoadMessageAboutUser];
[self checkAppUpDataWithshowOption:NO];
[SZNotificationCenter addObserver:self selector:@selector(phontoBroserPush:) name:kPushPhotoBrowserNotifitationName object:nil];
[SZNotificationCenter addObserver:self selector:@selector(playCallBackVideo:) name:kPresentVideoPlayerNotifitationName object:nil];
[self.window makeKeyAndVisible];
return YES;
}
好了付魔,我們著重說一下這里兩個(gè)通知聊品。
分別是收到圖片查看的通知、視頻播放的通知
這里以查看圖片舉列几苍。我們認(rèn)為查看圖片是push到一個(gè)新頁面中去翻屈。
因此我們需要找到導(dǎo)航控制器
根據(jù)之前所說,當(dāng)前結(jié)構(gòu)中只有一個(gè)導(dǎo)航控制器就是Root
+ (UINavigationController *)rootNavigationController
{
HHAppDelegate *app = (HHAppDelegate *)[UIApplication sharedApplication].delegate;
return (UINavigationController *)app.window.rootViewController;
}
找到控制器后就變得簡單的多了妻坝。
我們開始實(shí)現(xiàn):
這里我們對傳來的數(shù)據(jù)格式由一定要求伸眶,類似于這樣的一個(gè)格式。
@{@"imageInfo":@[HHImageInfo*info,HHImageInfo *info2],@"index":@3};
原因:圖片一般有高清圖片和非高清圖片兩種格式刽宪。以及圖片有可能帶有文字描述厘贼。因此引入ImageInfo。
- (void)phontoBroserPush:(NSNotification *)note
{
self.imageArr = [NSMutableArray arrayWithCapacity:0];
[self.imageArr removeAllObjects];
NSDictionary *dic = note.object;
NSArray *arr = dic[@"imageInfo"];
NSInteger index = [dic[@"index"] integerValue];
self.imageIndex = index;
for(HHImageInfo *info in arr)
{
//[self.imageArr addObject:info.url];
MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:info.url]];
photo.caption = info.desc;
[self.imageArr addObject:photo];
}
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self];
browser.zoomPhotosToFill = YES;
browser.displayNavArrows = YES;
browser.displayActionButton = NO;
browser.alwaysShowControls = NO;
browser.autoPlayOnAppear = YES;
[browser setCurrentPhotoIndex:index];
[[HHAppDelegate rootNavigationController]pushViewController:browser animated:YES];
}
到這里圣拄,其他開發(fā)人員在進(jìn)行模塊開發(fā)的時(shí)候需要圖片瀏覽器的時(shí)候直接發(fā)送通知即可集成嘴秸。
HHImageInfo中
@property (nonatomic,strong)NSString *desc;
@property (nonatomic,strong)NSString *url;
//@property (nonatomic,assign)NSInteger imageIndex;
+ (NSString *)getHUDImageUrl:(NSString *)smallImageUrl;
實(shí)現(xiàn):
+ (NSString *)getHUDImageUrl:(NSString *)smallImageUrl
{
NSMutableString *str = [NSMutableString stringWithString:smallImageUrl];
NSRange substr = [str rangeOfString:@"_thu"];
while (substr.location != NSNotFound)
{
[str replaceCharactersInRange:substr withString:@""];
substr = [str rangeOfString:@"_thu"];
}
[str insertString:@"" atIndex:0];
return str;
}
4. 其他配置
4.1 rootViewController -- 所有頁面均繼承該控制器
我通常在RootViewController實(shí)現(xiàn)以下方法以供調(diào)用
/**
* 顯示沒有數(shù)據(jù)頁面
*/
-(void)showNoDataImage;
/**
* 移除無數(shù)據(jù)頁面
*/
-(void)removeNoDataImage;
/**
* 需要登錄
*/
- (void)showShouldLoginPoint;
/**
* 加載視圖
*/
- (void)showLoadingAnimation;
/**
* 停止加載
*/
- (void)stopLoadingAnimation;
/**
* 分享頁面
*
* @param url url
* @param title 標(biāo)題
*/
- (void)shareUrl:(NSString *)url andTitle:(NSString *)title;
- (void)goLogin;
/**
* 狀態(tài)欄
*/
- (void)initStatusBar;
- (void)showStatusBarWithTitle:(NSString *)title;
- (void)changeStatusBarTitle:(NSString *)title;
- (void)hiddenStatusBar;
具體實(shí)現(xiàn)方法請看demo
2016.04.29
- 關(guān)于中文文件夾:咱們提倡使用英文。但是不反對中文.
樓主認(rèn)為所有便于開發(fā)的操作都是可以允許的售担。
我接手過那么幾個(gè)項(xiàng)目,內(nèi)部蹩腳(或高端)的英文讓我猝不及防署辉。我多想他用的是拼音我就不會理解的那么困難了族铆。
關(guān)于pod:demo只是提供了一種思路與步驟,僅做參考哭尝。繼承第三方庫方式就看個(gè)人了哥攘。
關(guān)于stroyboad:不利于團(tuán)隊(duì)開發(fā)。