极客班 iOS 应用开发实战(四)

21. iOS应用框架(上)

主要讲解了 Project 文件目录整理分类,Storyboard 和代码结合使用,Constraints 的使用。稍后在项目里面实践一下。

有提到 [UIScreen mainScreen].bounds 的问题。

我之前就研究过,见:http://gewill.org/2015/08/04/UIScreen-mainScreen-bounds-in-Xcode-7-Beta-4/

23. iOS应用框架(下)

主要演示 Storyboard 的自动布局和代码管理跳转和动画过度。但是总感觉有点基本上是在 AppDelegate 中管理 Storyboard 的跳转关系,实不如 Storyboard references 清晰明了,且不妨碍分工开发。

代码如下:

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
//  AppDelegate.h
// BLDemo05


#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


/**
* 声明一个 UINavigationController 属性,作为 rootViewController
*/
@property (strong, nonatomic) UINavigationController *loginNavigationVC;


/**
* 使用代码加载 Login.storyboard 的函数
*/
- (void)loadLoginView;
/**
* 使用代码加载 Login.storyboard 的函数
*/
- (void)loadMainView;

@end


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//  AppDelegate.m
// BLDemo05


#import "AppDelegate.h"
#import "BLUserInfoViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (void)loadLoginView {
// 读取 Login.storyboard, 压入 rootViewController(loginNavigationVC)
UIStoryboard *loginStoryboard
= [UIStoryboard storyboardWithName:@"Login"
bundle:[NSBundle mainBundle]];
self.loginNavigationVC = loginStoryboard.instantiateInitialViewController;
self.window.rootViewController = self.loginNavigationVC;
}


- (void)loadMainView {

// 1 - 读取 UserInfo.storyboard, 压入 BLUserInfoViewController(userInfoVC), 一并压入 UINavigationController(navc)
UIStoryboard *userInfoStoryboard
= [UIStoryboard storyboardWithName:@"UserInfo" bundle:[NSBundle mainBundle]];

BLUserInfoViewController *userInfoVC
= [userInfoStoryboard instantiateViewControllerWithIdentifier:@"BLUserInfoViewController"];

UINavigationController *navc
= [[UINavigationController alloc] initWithRootViewController:userInfoVC];

// 2 - 添加 UINavigationController 标题
navc.tabBarItem.title = @"userInfo";

// 3 - 新建一个 UITabBarController(tabBarC),并设为 rootViewController
UITabBarController *tabBarC = [[UITabBarController alloc] init];
tabBarC.viewControllers = @[navc];

self.window.rootViewController = tabBarC;

// 4 - 添加过度效果
[self.window addSubview:self.loginNavigationVC.view];

[UIView animateWithDuration:0.3
animations:^{
self.loginNavigationVC.view.alpha = 0;
self.loginNavigationVC.view.frame = CGRectZero;
self.loginNavigationVC.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
} completion:^(BOOL finished) {
self.loginNavigationVC = nil;
}];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

// App 启动,调用 loadLoginView 函数
[self loadLoginView];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

@end