代碼創建UIWindow對象
Xcode7之后使用代碼創建UIWindow對象:
//創建UIWindow對象
self.window =[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//給window設置背景顏色(白色)
self.window.backgroundColor = [UIColorwhiteColor];
//使window顯示
[self.window makeKeyAndVisible];
//創建一個視圖控制器
UIViewController *VC = [[UIViewController alloc] init];
//給window指定根視圖控制器
self.window.rootViewController = VC;
自動創建UIWindow對象
l1)先執行Main函數,執行UIApplicationMain()創建代理
l2)看項目配置文件info.plist里面的StoryBoard的name
l3)根據這個name找到對應的StoryBoard,加載StoryBoard
l4)在加載的時候創建一個window。
UIView功能
?1)管理矩形區域里的內容
?2)處理矩形區域中的事件
?3)子視圖的管理
?4)實現UIView動畫
?5)UIView作為父類,子類也具有這些功能
如何確定一個矩形
1.首先確定這個矩形的位置。(iOS中以左上角為坐標原點)
2.其次確定這個矩形的大小。
iOS中通過確定一個矩形的左上角的點(x,y)以及矩形的 寬(width)和高(height)來確定一個矩形。
坐標系相關數據類型
坐標:
struct CGPoint {CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
大小:
struct CGSize {CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
矩形:
struct CGRect {CGPoint origin;
CGSize size;
};
typedef structCGRect CGRect;
iOS中使用CGRect類型確定矩形位置以及大小
創建UIView
代碼:
//開辟空間創建UIView對象
//設置frame確定UIView對象的位置及大小
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//設置UIView對象的屬性:設置背景顏色
view.backgroundColor = [UIColor redColor];
//將創建好的UIView對象添加到window上顯示
[self.windowaddSubview:view];
第二節:
UILabel:
?UILabel(標簽):是顯?示?文本的控件。在App中UILabel是出現頻率最?高的控件
?UILabel是UIView?子類,作為?子類?一般是為了擴充?父類的 功能UILabel擴展了?文字顯?示的功能,UILabel是能顯?示?文 字的視圖。
創建UILabel與創建UIView的步驟很相似。
1、開辟空間并初始化(如果本類有初始化?方法,則使?用?自?己的初 始化?方法;否則使?用?父類的)。
2、設置?文本控制相關的屬性
3、添加到?父視圖上,?用以顯?示
4、 釋放所有權(只是做了引?用計數-1)
UILabel *userNameLabel = [[UILabel alloc]
initWithFrame:CGRectMake(30, 100, 100, 30)]; userNameLabel.text = @“?用戶名”;
[containerView addSubview:userNameLabel];
[userNameLabel release];
注:containerView已經添加到根視圖上
?UITextField(輸入框):是控制?本輸入和顯示的控件。在App中UITextField出現頻率也比較高。
?iOS系統借助虛擬鍵盤實現輸入,當點擊輸入框,系統會自動調出鍵盤, ?便你進一步操作。在你不需要輸入的時候,可以使用收回鍵盤的?法,收回彈出的鍵盤。
?UITextField和UILabel相比,UILabel主要用于文字顯示,不能編輯,UITextField允許用戶編輯文字(輸?)
創建UITextField與創建UILabel的步驟很相似。
1、開辟空間并初始化(如果本類有初始化?方法,則使?用?自?己的初始化?方法;否則使?用?父類的)。
2、設置?文本顯?示、輸?入等相關的屬性
3、添加到?父視圖上,?用以顯?示
4、釋放對象所有權
//使用初始化方法創建對象
UITextField *userNameTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(100, 100, 190, 30)];
//設置邊框?風格
userNameTextField.borderStyle= UITextBorderStyleRoundedRect;
//設置占位符
userNameTextField.placeholder
= @“手機號/郵箱";[containerViewaddSubview:userNameTextField];[userNameTextField release];
?UIButton(按鈕):是響應用戶點擊的控件。在App中UIButton是 出現頻率很高的控件。
?UIButton與UILabel、UITextField側重點不同,側重于處理用戶交互 事件。當然UIButton類也提供了一些方法控制按鈕外觀。
創建UIButton與創建UILabel、UITextField、UIView的步驟很相似。
1、創建button對象(如果本類有初始化方法,則使用自己的初始化方法;否則使用父類的)。
2、設置按鈕相關的屬性
3、為按鈕添加點擊事件
4、添加按鈕到父視圖上,用以顯示
5、按鈕無需釋放(一般情況創建UIButton都使用自己的便利構造器方法,無需釋放對象的所有權)
//便利構造器?方法創建對象
UIButton*loginButton = [UIButton buttonWithType:UIButtonTypeSystem];loginButton.frame = CGRectMake(30, 200, 60,30);
//設置button的標題
[loginButton setTitle:@"登錄" forState:UIControlStateNormal];
//添加點擊事件
[loginButtonaddTarget:self action:@selector(login:)forControlEvents:UIControlEventTouchUpInside];[containerView addSubview:loginButton];
UIImageView是iOS中用于顯示圖片的類,iOS中幾乎所有看到的 圖片,都是由這個類來顯示的。
//圖?片?文件路徑
NSString *path= [[NSBundle mainBundle] pathForResource:@"1"ofType:@"jpg"];
//創建?一個UIImage對象,使用initWithContentOfFile:方法
UIImage *image= [UIImage imageWithContentsOfFile:path];//創建一個UIImageView對象,使用initWithImage:方法
UIImageView*imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame= CGRectMake(100, 100, 100, 100);[self.view addSubview:imageView];
?animationImages//設置?一組動態圖?片
?animationDuration//設置播放?一組動態圖?片的時間
?animationRepeatCount//設置重復次數
?startAnimating//開始動畫
?stopAnimating//結束動畫
第三節:
LTView的具體使用
//創建LTView對象
LTView*usernameView = [[LTView alloc] initWithFrame:CGRectMake(40,100, 300,50)];usernameView.leftLabel.text =@"?用戶名:";usernameView.rightField.placeholder = @"請輸入用戶名";[usernameView.rightField becomeFirstResponder];[backViewaddSubview:usernameView];//backView是LTView添加到的父視圖
[usernameViewrelease];
視圖控制器指定自定義View
如何設置
-(void)loadView{
[superloadView];
self.myView =[[MyView alloc] initWithFrame:self.view.frame];
self.view =self.myView;
[self.myViewrelease];
}
#pragma mark視圖加載完畢
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor= [UIColor grayColor];NSLog(@"%s%d",__FUNCTION__, __LINE__);
//為自定義視圖里面的按鈕添加點擊事件[self.myView.buttonaddTarget:self action:@selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark點擊?方法
-(void)buttonAction{
NSLog(@"點擊了?自定義視圖?里?面的按鈕");
}
#pragma mark內存警告?方法
-(void)didReceiveMemoryWarning {
//即使沒有顯?示在window上,也不會?自動的將self.view釋放。
[super didReceiveMemoryWarning];
NSLog(@"%s%d", __FUNCTION__, __LINE__);
// Dispose ofany resources that can be recreated.
}
第四節:
觸摸事件的處理方法:
在給定的觸摸階段中,如果發生新的觸摸動作或已有的觸摸動作發生變化,應用程序就會發送這些消息;
當一個手指或多個手指觸碰屏幕時,發送touchesBegan:withEvent:消息。
當一個手指或多個手指在屏幕上移動時,發送touchesMoved:withEvent:消息。
當一個手指或多個手指離開屏幕時,發送touchesBegan:withEvent:消息。
手勢識別器
1 . UIapGestureRecognizer是輕拍?勢識別器,能識別輕拍操作
2.U I L o n g P r e s s G e s t u r e R e c o
g n i z e r是長按?勢識別器,能識別長按操作
3 . U I R o t a t i o n G e s t u r e R e c o g n i z
e r是旋轉?勢識別器,能識別旋轉操作
4 .U I P i n c h G e s t u r e R e c o g n i z e r是捏合?勢識別器,能識別捏合操作
5 .U I P a n G e s t u r e R e c o g n i z e r是平移?勢識別器,能識別拖拽操作
6 . U I S w i p e G e s t u r e R e c o g n i z e r是輕掃?勢識別器,能識別拖拽操作
7 . U I S c r e e n E d g e P a n G e s t u r e R e c
o g n i z e r是屏幕邊緣輕掃識別器
View的transform屬性:
transform是view的一個重要屬性,他在矩陣層面上改變view的顯示狀態,能實現view的縮放、旋轉、平移、等功能。
平移:CGAffineTransformMakeTranslation;
縮放:CGAffineTransformMakeScale;
旋轉:CGAffineTransformMakeRottion;
第五節
UIControl常用方法:
1.添加一個事件:
參數說明:target為目標對象;action為方法選擇器;controlEvents為觸發事件。
(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
2.移除一個事件:
參數說明:target為目標對象;action為方法選擇器;
controlEvents為觸發事件。
- (void)removeTarget:(id)target action: (SEL)action
forControlEvents: (UIControlEvents)controlEvents;
UISlider?示例代碼:
創建一個UISlider對象,并且添加到self.view上
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.center.x- 100, imageView.frame.origin.y + imageView.frame.size.height + 50, 200,50)];
[self.view addSubview:slider];
[sliderrelease];
2.UISlider相關屬性設置
//設置slider的最?小值
slider.minimumValue = 0.0;
//設置slider的最?大值
slider.maximumValue = 2;
//設置劃過區域的顏?色
slider.minimumTrackTintColor = [UIColor redColor];
3.為UISlider添加事件:
//添加slider的事件
[slider addTarget:self action:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];
UISegmentedControl添加事件
1.為UISegmentedControl添加事件
[segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
2.UISegmentedControl事件的響應(通常我們和swich...case分?支語
句組合使用)
#pragma mark segmentedControl點擊事件
-(void)segmentedAction:(UISegmentedControl *)segmented
{
switch (segmented.selectedSegmentIndex) {
case 0:
[self.view insertSubview:self.redbelowSubview:segmented];
break;
case 1:
[self.view insertSubview:self.bluebelowSubview:segmented];
break;
case 2:
[self.view insertSubview:self.yellowbelowSubview:segmented];
break;
default:
break;
}
}
UIPageControl常用屬性和方法
?numberOfPages //指定頁面個數(即點的個數)
?currentPage //指定pageControl的值(即選中的點)
?addTarget:action:forControlEvents: //給slider添加
事件
注意:controlEvent為UIControlEventValueChanged
原因:分頁本質是通過數據管理分頁,所以使?用valueChanged
屬性來觸發事件,即數組下標變化
總結
1.UIControl是所有控制視圖的?父類。
2.UISwitch的狀態監測。
3.UISlider為滑塊控件,通過控制value來設置當前的值,通過?用來控制視
頻、?音頻等播放進度。
4.UISegmentedControl為分段控件,相當于?一組按鈕,不同的Item可以
設置不同的點擊事件。
5.UIPageControl的currentPage屬性的使用。
需要注意:添加的點擊事件通過UIControlEventValueChanged來觸發事件
第六節
UIScrollView的創建
#define WIDTH self.view.frame.size.width
#define HEIGHTself.view.frame.size.height
// 1.創建?一個和屏幕等尺?寸的UIScrollView
UIScrollView*scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT)];
// 2.設置背景顏?色
scrollView.backgroundColor= [UIColoryellowColor];
// 3.把scrollView放到self.view上顯示
[self.viewaddSubview:scrollView];
// 4.內存管理
[scrollViewrelease];
UIScrollView協議方法
?當我們簽好協議,設置好代理人之后,我們就可以使用
UIScrollView的協議方法了,它的協議方法分為兩部分:
?一是監控滾動時候的狀態。
?二是控制視圖的縮放。
總結
?本節主要是為了讓大家了解滾動視圖的創建和基本的使用
方法,并且配合UIPageControl實現關聯使用。
?UIScrollView主要的屬性contentSize用來控制視圖的滾動
范圍,視圖的變化顯示主要由contentOffset偏移量來控
制。
?UIPageControl一般會和UIScrollView一同使用。
第七節
UINavigationController
?
UINavigationController:導航控制器,,是iOS中最常?用的多視圖控制器之一,用它來管理多個視圖控制器。
?
導航控制器可以稱為是,管理控制器的控制器,主要管理有層
次遞進關系的控制器。
創建
//創建根視圖
RootViewController *rootVC =
[[RootViewController alloc] init];
//創建導航控制器 把rootVC作為導航控制器的根視圖控制器
UINavigationController *navi =
[[UINavigationController alloc]
initWithRootViewController:rootVC];
//設置導航為window的根視圖
self.window.rootViewController = navi;
//內存管理
[rootVC release];
[navi release];
導航欄半透明效果
//導航欄半透明效果(iOS7以后 默認為YES)
//當半透明效果開啟時 屏幕左上?角為坐標原點
//關閉時 導航欄左下?角為坐標原點
self.navigationController.navigation
Bar.translucent = YES;
//創建?一個View
UIView *view = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 100,100)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
[view release];
頁面跳轉工作原理:
?
UINavigationController通過棧的方式管理控制器的切換,控制入棧和出棧來展示各個視圖控制器。
?UINavigationController的ContentView里始終顯示棧頂控制器的view。
?viewControllers屬性是一個可變數組(NSMutableArray)存儲了棧中的所有被管理的控制器,入棧的時候,使用addObject把新的視圖控制器對象添加到數組末尾,出棧時removeLastObject移除數組末尾的試圖控制器對象。
?navigationController屬性,父類中的屬性,每個在棧中的控制器,都能通過此屬性,獲取自己所在UINavigationController對象。
工作原理
? 棧的特點:先進后出,后進先出。
? 棧頂為當前顯?示的視圖控制器。
模態進入下一頁
- (void)next
{
//?頁?面跳轉
//模態(modal)
// 1.創建第二頁對象
SecondViewController *secVC = [[SecondViewControlleralloc]init];
// 2.設置過渡動畫(有默認值,可以不設置)
secVC.modalTransitionStyle =UIModalTransitionStyleCoverVertical;
// 3.模態控制器
//參數1:第二頁對象
//參數2:是否使用動畫
//參數3:模態完成后執行的block
[self presentViewController:secVC animated:YEScompletion:^{
}];
// 4.內存管理
[secVC release];
}
總結
1.UINavigationController的創建
2.通過導航跳轉頁?面
3.自定義NavigationBar
4.模態
第八節:
回顧協議六步
第一步:聲明協議
第二步:聲明代理人
第三步:執行協議方法
第四步:簽訂協議
第五步:指定代理人
第六步:實現協議方法
代碼演示
#warning第1步
//在SecondViewController.h?里聲明協議
@protocol SecondViewControllerDelegate
-(void)changeValue:(NSString *)name;
@end
@interface SecondViewController : UIViewController
#warning第2步
//聲明代理人
@property(nonatomic,assign)id
elegate>secondDelegate;
@end
#warning第3步
-(void)buttonAction:(UIButton *)button
{
//執?行協議方法
[self.secondDelegate
changeValue:self.textField.text];
[self.navigationController
popViewControllerAnimated:YES];
}
#warning第4步
//FirstViewController簽訂協議
@interface FirstViewController :
UIViewController
#warning第5步
-(void)buttonAction:(UIButton *)button
{
SecondViewController *secondVC =
[[SecondViewController alloc] init];
//指定當前對象為代理?人
secondVC.secondDelegate = self;
secondVC.contents = self.label.text;
[self.navigationController
pushViewController:secondVC animated:YES];
[secondVC release];
}
#warning第6步
//實現協議方法,并接收傳過來的值
-(void)changeValue:(NSString *)name
{
self.label.text = name;
}
注意
@protocol SecondViewControllerDelegate
@required //必須要實現的?方法,默認是必須實現
-(void)changeValue:(NSString *)name;
@optional //可選實現的協議方法
-(void)changeColor:(UIColor *)color;
@end
block傳值兩種方式
? 方式一:使用block屬性實現回調傳值
? 方式二:在方法中定義block實現回調傳值
方法中定義block
? 創建一個RootViewController并添加一個button和一個label
? 封裝一個NSObject類
? 實現點擊button將一個NSInteger類型的10086數字傳入NSObject中,
? 將其轉換為NSString類型
? 用block將轉換完的字符串回傳給RootViewController并顯示在label上
提示:使用在方法中封裝一個block來回調傳值更方便
#warning第一步
//在AppTool.h中重定義void(^)(NSString *string)類型
的別名為AppToolBlock
typedef void(^AppToolBlock)(NSString *string);
#warning第二步
//聲明方法,在方法中封裝block
-(void)sendNumber:(NSInteger )number andBlock:
(AppToolBlock)block;
#warning第三步
//在AppTool.m實現方法,并執行block
-(void)sendNumber:(NSInteger )number andBlock:
(AppToolBlock)block;
{
NSString *string = [NSString
stringWithFormat:@"%ld",number];
block(string);
}
-(void)buttonAction:(UIButton *)button
{
#warning第四步
AppTool *appTool = [[AppTool alloc] init];
//執行方法,實現block并接收回傳過來的string值
[appTool sendNumber:10086 andBlock:^(NSString
*string) {
self.label.text = string;
}];
}
總結
使用屬性傳值解決從前往后傳值的問題
使用協議/代理解決從后往前傳值的問題
使用__block修飾變量來解決block循環引用問題
注意:關于block內存管理上的三個區域,在arc和?非arc下還是有區別的,請學員自行打印驗證結果
第九節
代碼創建UITabBarController
代碼:
// 1.創建UITabBarController對象
UITabBarController *tabBarController =
[[UITabBarController alloc] init];
// 2.將TabBarController管理的視圖控制器放到?一個數組中
NSArray *viewControllers = [NSArray
arrayWithObjects:firstNav, secondNav,thirdNav,fourthNav, nil];
// 3.設置TabBarController的?子視圖控制器數組
mainTabBarVC.viewControllers = viewControllers;
// 4.將根視圖控制器設置成TabBarController
[self.window setRootViewController:mainTabBarVC]
程序的添加過程
UITabBarController創建完成后有一句關鍵代碼
//將根視圖控制器設置成TabBarController
[self.window setRootViewController:mainTabBarVC];
程序的添加過程:
UIWindow—>UITabBarcontroller—>UINavigationController—>UIViewController
UITabBar
?UITabBar包含多個UITabBarItem,每一個UITabBarItem對
應一個UIViewController。UITabBar的高度是49。
? 系統最多只顯示5個UITabBarItem,當UITabBarItem超過5個時系統會自動增加一個更多按鈕,點擊更多按鈕沒有在
底部出現的按鈕會以列表的形式顯示出來
?UITabBar的屬性:tintColor、barTintColor、圖像設置
等。
UITabBarItem的圖?片處理
代碼:
//未選中的圖片
UIImage *secondNormalImage = [UIImage
imageNamed:@“carGary”];
//圖片不被渲染,保持圖片本?身的樣子
secondNormalImage = [secondNormalImage
imageWithRenderingMode:UIImageRenderingModeAlwaysO
riginal];
//選中時的圖片
UIImage *secondSelectedImage = [UIImage
imageNamed:@"carRed"];
secondSelectedImage = [secondSelectedImage
imageWithRenderingMode:UIImageRenderingModeAlways
Original];
UIAppearance
代碼:
//設置全局外觀
//通過[UITabBar appearance]得到當前應用的UITabBar對象來設置tabBar的外觀
//注意:設置全局外觀最好在appDelegate里,否則會無效
[[UITabBar appearance] setBarTintColor:[UIColorcyanColor]];
[[UITabBar appearance] setTintColor:[UIColorbrownColor]];
//改變導航欄外觀顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor
lightGrayColor]];
//改變導航欄字體顏色
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor
redColor],NSForegroundColorAttributeName,[UIFontsystemFontOfSize:17],NSFontAttributeName, nil]];
總結
?UITabBarController是項目開發中常見的布局樣式,與
UINavigationController不同,它的viewControllers都是并列
的;?而UINavigationController的則是層次性的。
?UITabBar通常都會定義外觀以適應程序風格,必要時會完全自定義
第十節
UITableView
?UITableView繼承于UIScrollView,可以滾動。
?UITableView的每一條數據對應的單元格叫做Cell,是UITableViewCell的一個對象,繼承于UIView。
?UITableView可以分區顯示, 每一個分區稱為section,每一行稱為row, 編號都從0開始。
? 系統提供了一個專門的類來整合section和row,叫做NSIndexPath。
UITableViewDataSource協議方法的實現代碼
// tableView每個分區要顯示的行數
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
return 100;
}
// tableView每次要顯示一個cell都會調用這個方法獲取
- (UITableViewCell *)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"reuse"] autorelease];
cell.textLabel.text = @"標題";
return cell;
}
UITableView重用cell的流程
?1.當一個cell被滑出屏幕,這個cell會被系統放到相應的重?用池中。
?2.當tableView需要顯?示一個cell,會先去重?用池中嘗試獲取一個
cell(紅色的cell)。
?3.如果重用池沒有cell,就會創建一個cell(黃色的cell)。
?4.取得cell之后會重新賦值進行使用。
UITableView和數組
@interface ViewController ()
UITableViewDelegate>
//數組屬性,用來和tableView結合使用
@property (nonatomic, retain) NSMutableArray*sourceArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化數組
self.sourceArr = [NSMutableArray arrayWithObjects:
@"張三", @"李四", @"王五", @"趙六", nil];
}
總結
1. UITableView的基本概念和創建方法
2. UITableView的重用機制
3.常用的UITableView的協議方法的使用
第十一節
UITableView編輯步驟
UITableView編輯步驟如下:
一.讓TableView處于編輯狀態
二.協議設定
1.確定Cell是否處于編輯狀態
2.設定Cell的編輯樣式(刪除、添加)
3.編輯狀態進行提交
UITableView編輯步驟如下:
一.讓TableView處于編輯狀態
- (void)setEditing:(BOOL)editinganimated:(BOOL)animated
二.協議設定
1.確定Cell是否處于編輯狀態
- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:
(NSIndexPath *)indexPath
2.設定Cell的編輯樣式(刪除、添加)
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
3.編輯狀態進行提交
-(void) tableView:(UITableView *) tableViewcommitEditingStyle:
(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:
(NSIndexPath *)indexPath
UITableViewController
一:UITableViewController繼承自UIViewController,自帶
一個tableView
二:self.view不是UIView而是UITableView
三:datasource和delegate默認都是
self(UITableViewController)
四:開發中只需要建立UITableViewController子類
總結
1.UITableView編輯的寫法
2.UITableViewController
第十二節
自定義Cell
為什么需要自定義Cell?
? 在前期我們學過自定義視圖,即創建一個類繼承于UIView
或者其他的視圖,在自定義類中創建其子視圖,這樣就會
形成一個新的自定義視圖。
? 系統提供的cell滿足不了復雜的樣式,因此:自定義Cell
和自定義視圖?一樣,自己創建一種符合我們需求的Cell并
使用這個Cell
?自定義Cell步驟:
? 創建?一個類繼承于UITableViewCell。
? 實現UITableViewCell的初始化?方法:
-(instancetype)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier。
? 確保所有的你想添加的子視圖都在自定義Cell的初始化方法中創建,由于UITableView的重用機制,一個Cell在第一次創建成功并用于下一次顯示的時候,不會再走初始化方法,這樣
可以避免子視圖的重復創建。
? 在Cell的子視圖創建成功后,將子視圖設置為屬性,類似于
UITableViewCell所自帶的textLabel和detailTextLabel屬性。便于在UITableView的協議中給自定義視圖賦值。
Model的使用
創建步驟:
? 創建一個類并繼承于NSObject
? 添加和字典中對應的屬性
? 在視圖控制器中將字典通過KVC為Model賦值
? 將Model對象添加到數組中并刷新TableView
判斷多種Cell一
Model *model = [self.tableArray
objectAtIndex:indexPath.row];
//根據model屬性劃分
if (model.type == 0) {
FirstTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:firstIdentify];
return cell;
}
if (model.type == 1) {
SecondTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:secondIdentify];
return cell;
}
判斷多種Cell二
//第一行顯示第一種Cell
if (indexPath.row == 0) {
FirstTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:firstIdentify];
return cell;
}
//第二行顯示第二種Cell
if (indexPath.row == 1) {
SecondTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:secondIdentify];
return cell;
}
圖片自適應高度
UIImage *aImage = [UIImageimageNamed:@"1.png"];
//獲得圖片真實高度和寬度
CGFloat height = aImage.size.height;
CGFloat width = aImage.size.width;
//縮放后寬度固定為200
CGFloat scale = width / 200;
CGFloat realHeight = height / scale;
[self.myImageView setFrame:CGRectMake(0, 0,
200,realHeight)];
總結
? 自定義Cell
?Model類的創建和使用
? 多種Cell混合使用
?Cell的自適應高度
第十三節
視圖控制器獨有初始化方法:
- (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
?nibNameOrNil:當前控制器相關聯的nib文件,如果寫nil默認為同名文件
?nibBundleOrNil:當前應用程序包所在文件,如果寫nil默認為mainBundle
?在我們使?用init初始化(或者new直接創建)控制器的時候,會自動執行
控制器的此方法,參數均以默認值執行,所以可不寫。
自動布局
自動布局:autoLayout,不給View固定的位置,通過某些規則讓View自己適應自己的位置。
iOS6.0之后推出,在iOS8.0自動布局被大幅度優化,iOS9中新增了許多功能。
總結
1.xib可視化編程方式。
2.基礎控件的屬性操作以及與代碼的關聯方式。
3.自定義單元格的創建和使用。
4.自動布局的使用。
第十四節
StoryBoard注意事項
? 在AppDelegate的
-application: didFinishLaunchingWithOptions:
方法中不要再用代碼初始化一個window。
? 將創建好的Storyboard在應?用程序配置General中設置為MainInterface。
? 視圖添加與控制和IB開發一樣。
利用StoryBoard繪制?自定義單元格
StoryBoard繪制單元格的時候要注意以下幾點:
1.創建?自定義cell時選中左側TableViewCell。
2.繪制自定義UI界?面。
3.設置重用標識符。
4.將StoryBoard文件關聯至對應的UITableViewController和
UITableViewCell子類(自己創建的類)。
5.在UITableViewController中完成代碼書寫:設置section和row數量,
設置cell,根據實際情況調整cell的高度。
注意:cell不再需要注冊。
StoryBoard進行頁面跳轉
StoryBoard頁面跳轉分為兩種。
?1、代碼方式:使用代碼通過控制器標識來跳轉。比如在當前頁
面的某一個事件中跳轉到一個標識為“customVC”的控制器頁面
中:[self performSegueWithIdentifier:@"customVC"sender:nil];
?2、連線方式:直接使用拖拽可以給按鈕連線關聯兩個頁面:選中按鈕,按住control,從按鈕向下一級頁面連線。按鈕不需要添加響應方法。
自定義segue
?步驟一:新建一個類繼承自UIStoryboardSegue。
?步驟二:選中前一個控制器,按住control鼠標輔助完成連線,選擇custom。
?步驟三:選中自定義segue,設置segue的identifier以及關聯類。
?步驟四:在segue類里面重寫perform方法(界面間跳轉默認執行的方法),自定義跳轉效果。
@implementation CustomSegue
//頁面跳轉時,自定義segue會自動觸發此方法(重寫的系統方法)
-(void)perform
{
//獲取源控制器
UIViewController *v1 = (UIViewController
*)self.sourceViewController;
//獲取目標控制器
UIViewController *v2 = (UIViewController
*)self.destinationViewController;
//自定義頁面切換效果
[UIView transitionFromView:v1.view toView:v2.viewduration:
10 options:UIViewAnimationOptionTransitionCurlDown
completion:^(BOOL finished) {
//動畫完成后執行的部分
}];
}
sizeClasses
?設備對應關系如下:
?iPhone4S,iPhone5/5s,iPhone6,iPhone6s
? 豎屏:(w:Compact h:Regular)
? 橫屏:(w:Compact h:Compact)
?iPhone6 Plus/iPhone6s Plus
? 豎屏:(w:Compact h:Regular)
? 橫屏:(w:Regular h:Compact)
?iPad
? 豎屏:(w:Regular h:Regular)
? 橫屏:(w:Regular h:Regular)
總結
1.StoryBoard與xib在可視化編程中的對比。
2.可視化編程中頁?面傳值的方式。
3.自定義segue實現頁面跳轉動畫。
4.sizeClass適配。
第十五節
創建UICollectionViewFlowLayout
UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];
//設置每個item的大小
flowLayout.itemSize = CGSizeMake(100, 100);
//設置每個item的最小列間距(默認是10)
flowLayout.minimumInteritemSpacing = 10;
//設置每個item的最小行間距(默認是10)
flowLayout.minimumLineSpacing = 10;
//設置分區間隔 (上,左,下,右)
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10,10);
//設置UICollectionView的滑動方向
flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;
//頭部引用的尺寸
flowLayout.headerReferenceSize = CGSizeMake(100, 100);
//尾部引?用的尺寸
flowLayout.footerReferenceSize = CGSizeMake(100, 100);
設置代理
遵守代理協議:
@interface ViewController()
UICollectionViewDelegate>
@end
@implementation ViewController
指定代理人:
collectionView.delegate = self;
collectionView.dataSource = self;
創建item視圖對象
//創建item視圖對象
-(UICollectionViewCell *)collectionView: (UICollectionView*)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"collectionCELL"forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
返回頭部、尾部視圖樣式
UICollectionView不能像UITableView一樣直接指定頭部和尾部視
圖,需要注冊使用,最大的好處是添加了重用機制。
//注冊頭部視圖
[collectionView registerClass:[UICollectionReusableViewclass]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerView"];
//注冊尾部視圖
[collectionViewregisterClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footerView"];
第十六節
單例模式
? 單例模式是一種設計模式
? 實現: 定義一個單例類,保證程序中這個類只能創建唯
一的實例對象,實現資源共享
單例代碼展示
static DataHandle *handle = nil;
//單例類使用此方法,創建單例對象
+ (DataHandle *)shareInstance
{
if (nil == handle) {
//如果還沒有創建過對象,使用handle指向新創建的對象
handle = [[DataHandle alloc] init];
}
//如果已經創建過對象,則直接返回已經創建的對象
return handle;
}
注意事項
?1)操作單例對象的變量存儲在靜態區程序關閉后由系統自動
回收。
?2)單例對象存儲在堆區,不需要釋放程序,關閉后由系統自
動回收。
?3)變量和單例對象的生命周期與程序同步。
優勢
?1)在內存中只有一個對象,節省內存空間
?2)避免頻繁的創建銷毀對象,可以提高性能
?3)避免對共享資源的多重占用
?4)可以全局訪問
?5)降低模塊之間的耦合度,降低代碼的復雜度
框架設置
項目頁面框架步驟:
1、根據項目的頁面個數創建相同個數的視圖控制器
2、根據項目的頁面邏輯創建每個視圖控制器上的切換方法
3、根據項目的頁面功能創建每個視圖控制器上的功能方法