iOS --- UI 簡(jiǎn)單總結(jié)




代碼創(chuàng)建UIWindow對(duì)象

Xcode7之后使用代碼創(chuàng)建UIWindow對(duì)象:

//創(chuàng)建UIWindow對(duì)象

self.window =[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

//給window設(shè)置背景顏色(白色)

self.window.backgroundColor = [UIColorwhiteColor];

//使window顯示

[self.window makeKeyAndVisible];

//創(chuàng)建一個(gè)視圖控制器

UIViewController *VC = [[UIViewController alloc] init];

//給window指定根視圖控制器

self.window.rootViewController = VC;

自動(dòng)創(chuàng)建UIWindow對(duì)象

l1)先執(zhí)行Main函數(shù),執(zhí)行UIApplicationMain()創(chuàng)建代理

l2)看項(xiàng)目配置文件info.plist里面的StoryBoard的name

l3)根據(jù)這個(gè)name找到對(duì)應(yīng)的StoryBoard,加載StoryBoard

l4)在加載的時(shí)候創(chuàng)建一個(gè)window。

UIView功能

?1)管理矩形區(qū)域里的內(nèi)容

?2)處理矩形區(qū)域中的事件

?3)子視圖的管理

?4)實(shí)現(xiàn)UIView動(dòng)畫(huà)

?5)UIView作為父類,子類也具有這些功能

如何確定一個(gè)矩形

1.首先確定這個(gè)矩形的位置。(iOS中以左上角為坐標(biāo)原點(diǎn))

2.其次確定這個(gè)矩形的大小。

iOS中通過(guò)確定一個(gè)矩形的左上角的點(diǎn)(x,y)以及矩形的 寬(width)和高(height)來(lái)確定一個(gè)矩形。

坐標(biāo)系相關(guān)數(shù)據(jù)類型

坐標(biāo):

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類型確定矩形位置以及大小

創(chuàng)建UIView

代碼:

//開(kāi)辟空間創(chuàng)建UIView對(duì)象

//設(shè)置frame確定UIView對(duì)象的位置及大小

UIView *view =[[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

//設(shè)置UIView對(duì)象的屬性:設(shè)置背景顏色

view.backgroundColor = [UIColor redColor];

//將創(chuàng)建好的UIView對(duì)象添加到window上顯示

[self.windowaddSubview:view];

第二節(jié):

UILabel:

?UILabel(標(biāo)簽):是顯?示?文本的控件。在App中UILabel是出現(xiàn)頻率最?高的控件

?UILabel是UIView?子類,作為?子類?一般是為了擴(kuò)充?父類的 功能UILabel擴(kuò)展了?文字顯?示的功能,UILabel是能顯?示?文 字的視圖。

創(chuàng)建UILabel與創(chuàng)建UIView的步驟很相似。

1、開(kāi)辟空間并初始化(如果本類有初始化?方法,則使?用?自?己的初 始化?方法;否則使?用?父類的)。

2、設(shè)置?文本控制相關(guān)的屬性

3、添加到?父視圖上,?用以顯?示

4、 釋放所有權(quán)(只是做了引?用計(jì)數(shù)-1)

UILabel *userNameLabel = [[UILabel alloc]

initWithFrame:CGRectMake(30, 100, 100, 30)]; userNameLabel.text = @“?用戶名”;

[containerView addSubview:userNameLabel];

[userNameLabel release];

注:containerView已經(jīng)添加到根視圖上

?UITextField(輸入框):是控制?本輸入和顯示的控件。在App中UITextField出現(xiàn)頻率也比較高。

?iOS系統(tǒng)借助虛擬鍵盤實(shí)現(xiàn)輸入,當(dāng)點(diǎn)擊輸入框,系統(tǒng)會(huì)自動(dòng)調(diào)出鍵盤, ?便你進(jìn)一步操作。在你不需要輸入的時(shí)候,可以使用收回鍵盤的?法,收回彈出的鍵盤。

?UITextField和UILabel相比,UILabel主要用于文字顯示,不能編輯,UITextField允許用戶編輯文字(輸?)

創(chuàng)建UITextField與創(chuàng)建UILabel的步驟很相似。

1、開(kāi)辟空間并初始化(如果本類有初始化?方法,則使?用?自?己的初始化?方法;否則使?用?父類的)。

2、設(shè)置?文本顯?示、輸?入等相關(guān)的屬性

3、添加到?父視圖上,?用以顯?示

4、釋放對(duì)象所有權(quán)

//使用初始化方法創(chuàng)建對(duì)象

UITextField *userNameTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(100, 100, 190, 30)];

//設(shè)置邊框?風(fēng)格

userNameTextField.borderStyle= UITextBorderStyleRoundedRect;

//設(shè)置占位符

userNameTextField.placeholder

= @“手機(jī)號(hào)/郵箱";[containerViewaddSubview:userNameTextField];[userNameTextField release];

?UIButton(按鈕):是響應(yīng)用戶點(diǎn)擊的控件。在App中UIButton是 出現(xiàn)頻率很高的控件。

?UIButton與UILabel、UITextField側(cè)重點(diǎn)不同,側(cè)重于處理用戶交互 事件。當(dāng)然UIButton類也提供了一些方法控制按鈕外觀。

創(chuàng)建UIButton與創(chuàng)建UILabel、UITextField、UIView的步驟很相似。

1、創(chuàng)建button對(duì)象(如果本類有初始化方法,則使用自己的初始化方法;否則使用父類的)。

2、設(shè)置按鈕相關(guān)的屬性

3、為按鈕添加點(diǎn)擊事件

4、添加按鈕到父視圖上,用以顯示

5、按鈕無(wú)需釋放(一般情況創(chuàng)建UIButton都使用自己的便利構(gòu)造器方法,無(wú)需釋放對(duì)象的所有權(quán))

//便利構(gòu)造器?方法創(chuàng)建對(duì)象

UIButton*loginButton = [UIButton buttonWithType:UIButtonTypeSystem];loginButton.frame = CGRectMake(30, 200, 60,30);

//設(shè)置button的標(biāo)題

[loginButton setTitle:@"登錄" forState:UIControlStateNormal];

//添加點(diǎn)擊事件

[loginButtonaddTarget:self action:@selector(login:)forControlEvents:UIControlEventTouchUpInside];[containerView addSubview:loginButton];

UIImageView是iOS中用于顯示圖片的類,iOS中幾乎所有看到的 圖片,都是由這個(gè)類來(lái)顯示的。

//圖?片?文件路徑

NSString *path= [[NSBundle mainBundle] pathForResource:@"1"ofType:@"jpg"];

//創(chuàng)建?一個(gè)UIImage對(duì)象,使用initWithContentOfFile:方法

UIImage *image= [UIImage imageWithContentsOfFile:path];//創(chuàng)建一個(gè)UIImageView對(duì)象,使用initWithImage:方法

UIImageView*imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame= CGRectMake(100, 100, 100, 100);[self.view addSubview:imageView];

?animationImages//設(shè)置?一組動(dòng)態(tài)圖?片

?animationDuration//設(shè)置播放?一組動(dòng)態(tài)圖?片的時(shí)間

?animationRepeatCount//設(shè)置重復(fù)次數(shù)

?startAnimating//開(kāi)始動(dòng)畫(huà)

?stopAnimating//結(jié)束動(dòng)畫(huà)

第三節(jié):

LTView的具體使用

//創(chuàng)建LTView對(duì)象

LTView*usernameView = [[LTView alloc] initWithFrame:CGRectMake(40,100, 300,50)];usernameView.leftLabel.text =@"?用戶名:";usernameView.rightField.placeholder = @"請(qǐng)輸入用戶名";[usernameView.rightField becomeFirstResponder];[backViewaddSubview:usernameView];//backView是LTView添加到的父視圖

[usernameViewrelease];

視圖控制器指定自定義View

如何設(shè)置

-(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__);

//為自定義視圖里面的按鈕添加點(diǎn)擊事件[self.myView.buttonaddTarget:self action:@selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];

}

#pragma mark點(diǎn)擊?方法

-(void)buttonAction{

NSLog(@"點(diǎn)擊了?自定義視圖?里?面的按鈕");

}

#pragma mark內(nèi)存警告?方法

-(void)didReceiveMemoryWarning {

//即使沒(méi)有顯?示在window上,也不會(huì)?自動(dòng)的將self.view釋放。

[super didReceiveMemoryWarning];

NSLog(@"%s%d", __FUNCTION__, __LINE__);

// Dispose ofany resources that can be recreated.

}

第四節(jié):

觸摸事件的處理方法:

在給定的觸摸階段中,如果發(fā)生新的觸摸動(dòng)作或已有的觸摸動(dòng)作發(fā)生變化,應(yīng)用程序就會(huì)發(fā)送這些消息;

當(dāng)一個(gè)手指或多個(gè)手指觸碰屏幕時(shí),發(fā)送touchesBegan:withEvent:消息。

當(dāng)一個(gè)手指或多個(gè)手指在屏幕上移動(dòng)時(shí),發(fā)送touchesMoved:withEvent:消息。

當(dāng)一個(gè)手指或多個(gè)手指離開(kāi)屏幕時(shí),發(fā)送touchesBegan:withEvent:消息。

手勢(shì)識(shí)別器

1 . UIapGestureRecognizer是輕拍?勢(shì)識(shí)別器,能識(shí)別輕拍操作

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是長(zhǎng)按?勢(shì)識(shí)別器,能識(shí)別長(zhǎng)按操作

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是旋轉(zhuǎn)?勢(shì)識(shí)別器,能識(shí)別旋轉(zhuǎn)操作

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是捏合?勢(shì)識(shí)別器,能識(shí)別捏合操作

5 .U I P a n G e s t u r e R e c o g n i z e r是平移?勢(shì)識(shí)別器,能識(shí)別拖拽操作

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是輕掃?勢(shì)識(shí)別器,能識(shí)別拖拽操作

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是屏幕邊緣輕掃識(shí)別器

View的transform屬性:

transform是view的一個(gè)重要屬性,他在矩陣層面上改變view的顯示狀態(tài),能實(shí)現(xiàn)view的縮放、旋轉(zhuǎn)、平移、等功能。

平移:CGAffineTransformMakeTranslation;

縮放:CGAffineTransformMakeScale;

旋轉(zhuǎn):CGAffineTransformMakeRottion;

第五節(jié)

UIControl常用方法:

1.添加一個(gè)事件:

參數(shù)說(shuō)明:target為目標(biāo)對(duì)象;action為方法選擇器;controlEvents為觸發(fā)事件。

(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

2.移除一個(gè)事件:

參數(shù)說(shuō)明:target為目標(biāo)對(duì)象;action為方法選擇器;

controlEvents為觸發(fā)事件。

- (void)removeTarget:(id)target action: (SEL)action

forControlEvents: (UIControlEvents)controlEvents;

UISlider?示例代碼:

創(chuàng)建一個(gè)UISlider對(duì)象,并且添加到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相關(guān)屬性設(shè)置

//設(shè)置slider的最?小值

slider.minimumValue = 0.0;

//設(shè)置slider的最?大值

slider.maximumValue = 2;

//設(shè)置劃過(guò)區(qū)域的顏?色

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事件的響應(yīng)(通常我們和swich...case分?支語(yǔ)

句組合使用)

#pragma mark segmentedControl點(diǎn)擊事件

-(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 //指定頁(yè)面?zhèn)€數(shù)(即點(diǎn)的個(gè)數(shù))

?currentPage //指定pageControl的值(即選中的點(diǎn))

?addTarget:action:forControlEvents: //給slider添加

事件

注意:controlEvent為UIControlEventValueChanged

原因:分頁(yè)本質(zhì)是通過(guò)數(shù)據(jù)管理分頁(yè),所以使?用valueChanged

屬性來(lái)觸發(fā)事件,即數(shù)組下標(biāo)變化

總結(jié)

1.UIControl是所有控制視圖的?父類。

2.UISwitch的狀態(tài)監(jiān)測(cè)。

3.UISlider為滑塊控件,通過(guò)控制value來(lái)設(shè)置當(dāng)前的值,通過(guò)?用來(lái)控制視

頻、?音頻等播放進(jìn)度。

4.UISegmentedControl為分段控件,相當(dāng)于?一組按鈕,不同的Item可以

設(shè)置不同的點(diǎn)擊事件。

5.UIPageControl的currentPage屬性的使用。

需要注意:添加的點(diǎn)擊事件通過(guò)UIControlEventValueChanged來(lái)觸發(fā)事件

第六節(jié)

UIScrollView的創(chuàng)建

#define WIDTH self.view.frame.size.width

#define HEIGHTself.view.frame.size.height

// 1.創(chuàng)建?一個(gè)和屏幕等尺?寸的UIScrollView

UIScrollView*scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT)];

// 2.設(shè)置背景顏?色

scrollView.backgroundColor= [UIColoryellowColor];

// 3.把scrollView放到self.view上顯示

[self.viewaddSubview:scrollView];

// 4.內(nèi)存管理

[scrollViewrelease];

UIScrollView協(xié)議方法

?當(dāng)我們簽好協(xié)議,設(shè)置好代理人之后,我們就可以使用

UIScrollView的協(xié)議方法了,它的協(xié)議方法分為兩部分:

?一是監(jiān)控滾動(dòng)時(shí)候的狀態(tài)。

?二是控制視圖的縮放。

總結(jié)

?本節(jié)主要是為了讓大家了解滾動(dòng)視圖的創(chuàng)建和基本的使用

方法,并且配合UIPageControl實(shí)現(xiàn)關(guān)聯(lián)使用。

?UIScrollView主要的屬性contentSize用來(lái)控制視圖的滾動(dòng)

范圍,視圖的變化顯示主要由contentOffset偏移量來(lái)控

制。

?UIPageControl一般會(huì)和UIScrollView一同使用。

第七節(jié)

UINavigationController

?

UINavigationController:導(dǎo)航控制器,,是iOS中最常?用的多視圖控制器之一,用它來(lái)管理多個(gè)視圖控制器。

?

導(dǎo)航控制器可以稱為是,管理控制器的控制器,主要管理有層

次遞進(jìn)關(guān)系的控制器。

創(chuàng)建

//創(chuàng)建根視圖

RootViewController *rootVC =

[[RootViewController alloc] init];

//創(chuàng)建導(dǎo)航控制器 把rootVC作為導(dǎo)航控制器的根視圖控制器

UINavigationController *navi =

[[UINavigationController alloc]

initWithRootViewController:rootVC];

//設(shè)置導(dǎo)航為window的根視圖

self.window.rootViewController = navi;

//內(nèi)存管理

[rootVC release];

[navi release];

導(dǎo)航欄半透明效果

//導(dǎo)航欄半透明效果(iOS7以后 默認(rèn)為YES)

//當(dāng)半透明效果開(kāi)啟時(shí) 屏幕左上?角為坐標(biāo)原點(diǎn)

//關(guān)閉時(shí) 導(dǎo)航欄左下?角為坐標(biāo)原點(diǎn)

self.navigationController.navigation

Bar.translucent = YES;

//創(chuàng)建?一個(gè)View

UIView *view = [[UIView alloc]

initWithFrame:CGRectMake(0, 0, 100,100)];

view.backgroundColor = [UIColor blueColor];

[self.view addSubview:view];

[view release];

頁(yè)面跳轉(zhuǎn)工作原理:

?

UINavigationController通過(guò)棧的方式管理控制器的切換,控制入棧和出棧來(lái)展示各個(gè)視圖控制器。

?UINavigationController的ContentView里始終顯示棧頂控制器的view。

?viewControllers屬性是一個(gè)可變數(shù)組(NSMutableArray)存儲(chǔ)了棧中的所有被管理的控制器,入棧的時(shí)候,使用addObject把新的視圖控制器對(duì)象添加到數(shù)組末尾,出棧時(shí)removeLastObject移除數(shù)組末尾的試圖控制器對(duì)象。

?navigationController屬性,父類中的屬性,每個(gè)在棧中的控制器,都能通過(guò)此屬性,獲取自己所在UINavigationController對(duì)象。

工作原理

? 棧的特點(diǎn):先進(jìn)后出,后進(jìn)先出。

? 棧頂為當(dāng)前顯?示的視圖控制器。

模態(tài)進(jìn)入下一頁(yè)

- (void)next

{

//?頁(yè)?面跳轉(zhuǎn)

//模態(tài)(modal)

// 1.創(chuàng)建第二頁(yè)對(duì)象

SecondViewController *secVC = [[SecondViewControlleralloc]init];

// 2.設(shè)置過(guò)渡動(dòng)畫(huà)(有默認(rèn)值,可以不設(shè)置)

secVC.modalTransitionStyle =UIModalTransitionStyleCoverVertical;

// 3.模態(tài)控制器

//參數(shù)1:第二頁(yè)對(duì)象

//參數(shù)2:是否使用動(dòng)畫(huà)

//參數(shù)3:模態(tài)完成后執(zhí)行的block

[self presentViewController:secVC animated:YEScompletion:^{

}];

// 4.內(nèi)存管理

[secVC release];

}

總結(jié)

1.UINavigationController的創(chuàng)建

2.通過(guò)導(dǎo)航跳轉(zhuǎn)頁(yè)?面

3.自定義NavigationBar

4.模態(tài)

第八節(jié):

回顧協(xié)議六步

第一步:聲明協(xié)議

第二步:聲明代理人

第三步:執(zhí)行協(xié)議方法

第四步:簽訂協(xié)議

第五步:指定代理人

第六步:實(shí)現(xiàn)協(xié)議方法

代碼演示

#warning第1步

//在SecondViewController.h?里聲明協(xié)議

@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

{

//執(zhí)?行協(xié)議方法

[self.secondDelegate

changeValue:self.textField.text];

[self.navigationController

popViewControllerAnimated:YES];

}

#warning第4步

//FirstViewController簽訂協(xié)議

@interface FirstViewController :

UIViewController

#warning第5步

-(void)buttonAction:(UIButton *)button

{

SecondViewController *secondVC =

[[SecondViewController alloc] init];

//指定當(dāng)前對(duì)象為代理?人

secondVC.secondDelegate = self;

secondVC.contents = self.label.text;

[self.navigationController

pushViewController:secondVC animated:YES];

[secondVC release];

}

#warning第6步

//實(shí)現(xiàn)協(xié)議方法,并接收傳過(guò)來(lái)的值

-(void)changeValue:(NSString *)name

{

self.label.text = name;

}

注意

@protocol SecondViewControllerDelegate

@required //必須要實(shí)現(xiàn)的?方法,默認(rèn)是必須實(shí)現(xiàn)

-(void)changeValue:(NSString *)name;

@optional //可選實(shí)現(xiàn)的協(xié)議方法

-(void)changeColor:(UIColor *)color;

@end

block傳值兩種方式

? 方式一:使用block屬性實(shí)現(xiàn)回調(diào)傳值

? 方式二:在方法中定義block實(shí)現(xiàn)回調(diào)傳值

方法中定義block

? 創(chuàng)建一個(gè)RootViewController并添加一個(gè)button和一個(gè)label

? 封裝一個(gè)NSObject類

? 實(shí)現(xiàn)點(diǎn)擊button將一個(gè)NSInteger類型的10086數(shù)字傳入NSObject中,

? 將其轉(zhuǎn)換為NSString類型

? 用block將轉(zhuǎn)換完的字符串回傳給RootViewController并顯示在label上

提示:使用在方法中封裝一個(gè)block來(lái)回調(diào)傳值更方便

#warning第一步

//在AppTool.h中重定義void(^)(NSString *string)類型

的別名為AppToolBlock

typedef void(^AppToolBlock)(NSString *string);

#warning第二步

//聲明方法,在方法中封裝block

-(void)sendNumber:(NSInteger )number andBlock:

(AppToolBlock)block;

#warning第三步

//在AppTool.m實(shí)現(xiàn)方法,并執(zhí)行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];

//執(zhí)行方法,實(shí)現(xiàn)block并接收回傳過(guò)來(lái)的string值

[appTool sendNumber:10086 andBlock:^(NSString

*string) {

self.label.text = string;

}];

}

總結(jié)

使用屬性傳值解決從前往后傳值的問(wèn)題

使用協(xié)議/代理解決從后往前傳值的問(wèn)題

使用__block修飾變量來(lái)解決block循環(huán)引用問(wèn)題

注意:關(guān)于block內(nèi)存管理上的三個(gè)區(qū)域,在arc和?非arc下還是有區(qū)別的,請(qǐng)學(xué)員自行打印驗(yàn)證結(jié)果

第九節(jié)

代碼創(chuàng)建UITabBarController

代碼:

// 1.創(chuàng)建UITabBarController對(duì)象

UITabBarController *tabBarController =

[[UITabBarController alloc] init];

// 2.將TabBarController管理的視圖控制器放到?一個(gè)數(shù)組中

NSArray *viewControllers = [NSArray

arrayWithObjects:firstNav, secondNav,thirdNav,fourthNav, nil];

// 3.設(shè)置TabBarController的?子視圖控制器數(shù)組

mainTabBarVC.viewControllers = viewControllers;

// 4.將根視圖控制器設(shè)置成TabBarController

[self.window setRootViewController:mainTabBarVC]

程序的添加過(guò)程

UITabBarController創(chuàng)建完成后有一句關(guān)鍵代碼

//將根視圖控制器設(shè)置成TabBarController

[self.window setRootViewController:mainTabBarVC];

程序的添加過(guò)程:

UIWindow—>UITabBarcontroller—>UINavigationController—>UIViewController

UITabBar

?UITabBar包含多個(gè)UITabBarItem,每一個(gè)UITabBarItem對(duì)

應(yīng)一個(gè)UIViewController。UITabBar的高度是49。

? 系統(tǒng)最多只顯示5個(gè)UITabBarItem,當(dāng)UITabBarItem超過(guò)5個(gè)時(shí)系統(tǒng)會(huì)自動(dòng)增加一個(gè)更多按鈕,點(diǎn)擊更多按鈕沒(méi)有在

底部出現(xiàn)的按鈕會(huì)以列表的形式顯示出來(lái)

?UITabBar的屬性:tintColor、barTintColor、圖像設(shè)置

等。

UITabBarItem的圖?片處理

代碼:

//未選中的圖片

UIImage *secondNormalImage = [UIImage

imageNamed:@“carGary”];

//圖片不被渲染,保持圖片本?身的樣子

secondNormalImage = [secondNormalImage

imageWithRenderingMode:UIImageRenderingModeAlwaysO

riginal];

//選中時(shí)的圖片

UIImage *secondSelectedImage = [UIImage

imageNamed:@"carRed"];

secondSelectedImage = [secondSelectedImage

imageWithRenderingMode:UIImageRenderingModeAlways

Original];

UIAppearance

代碼:

//設(shè)置全局外觀

//通過(guò)[UITabBar appearance]得到當(dāng)前應(yīng)用的UITabBar對(duì)象來(lái)設(shè)置tabBar的外觀

//注意:設(shè)置全局外觀最好在appDelegate里,否則會(huì)無(wú)效

[[UITabBar appearance] setBarTintColor:[UIColorcyanColor]];

[[UITabBar appearance] setTintColor:[UIColorbrownColor]];

//改變導(dǎo)航欄外觀顏色

[[UINavigationBar appearance] setBarTintColor:[UIColor

lightGrayColor]];

//改變導(dǎo)航欄字體顏色

[[UINavigationBar appearance] setTitleTextAttributes:

[NSDictionary dictionaryWithObjectsAndKeys:[UIColor

redColor],NSForegroundColorAttributeName,[UIFontsystemFontOfSize:17],NSFontAttributeName, nil]];

總結(jié)

?UITabBarController是項(xiàng)目開(kāi)發(fā)中常見(jiàn)的布局樣式,與

UINavigationController不同,它的viewControllers都是并列

的;?而UINavigationController的則是層次性的。

?UITabBar通常都會(huì)定義外觀以適應(yīng)程序風(fēng)格,必要時(shí)會(huì)完全自定義

第十節(jié)

UITableView

?UITableView繼承于UIScrollView,可以滾動(dòng)。

?UITableView的每一條數(shù)據(jù)對(duì)應(yīng)的單元格叫做Cell,是UITableViewCell的一個(gè)對(duì)象,繼承于UIView。

?UITableView可以分區(qū)顯示, 每一個(gè)分區(qū)稱為section,每一行稱為row, 編號(hào)都從0開(kāi)始。

? 系統(tǒng)提供了一個(gè)專門的類來(lái)整合section和row,叫做NSIndexPath。

UITableViewDataSource協(xié)議方法的實(shí)現(xiàn)代碼

// tableView每個(gè)分區(qū)要顯示的行數(shù)

- (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section{

return 100;

}

// tableView每次要顯示一個(gè)cell都會(huì)調(diào)用這個(gè)方法獲取

- (UITableViewCell *)tableView:(UITableView*)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [[[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:@"reuse"] autorelease];

cell.textLabel.text = @"標(biāo)題";

return cell;

}

UITableView重用cell的流程

?1.當(dāng)一個(gè)cell被滑出屏幕,這個(gè)cell會(huì)被系統(tǒng)放到相應(yīng)的重?用池中。

?2.當(dāng)tableView需要顯?示一個(gè)cell,會(huì)先去重?用池中嘗試獲取一個(gè)

cell(紅色的cell)。

?3.如果重用池沒(méi)有cell,就會(huì)創(chuàng)建一個(gè)cell(黃色的cell)。

?4.取得cell之后會(huì)重新賦值進(jìn)行使用。

UITableView和數(shù)組

@interface ViewController ()

UITableViewDelegate>

//數(shù)組屬性,用來(lái)和tableView結(jié)合使用

@property (nonatomic, retain) NSMutableArray*sourceArr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//初始化數(shù)組

self.sourceArr = [NSMutableArray arrayWithObjects:

@"張三", @"李四", @"王五", @"趙六", nil];

}

總結(jié)

1. UITableView的基本概念和創(chuàng)建方法

2. UITableView的重用機(jī)制

3.常用的UITableView的協(xié)議方法的使用

第十一節(jié)

UITableView編輯步驟

UITableView編輯步驟如下:

一.讓TableView處于編輯狀態(tài)

二.協(xié)議設(shè)定

1.確定Cell是否處于編輯狀態(tài)

2.設(shè)定Cell的編輯樣式(刪除、添加)

3.編輯狀態(tài)進(jìn)行提交

UITableView編輯步驟如下:

一.讓TableView處于編輯狀態(tài)

- (void)setEditing:(BOOL)editinganimated:(BOOL)animated

二.協(xié)議設(shè)定

1.確定Cell是否處于編輯狀態(tài)

- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:

(NSIndexPath *)indexPath

2.設(shè)定Cell的編輯樣式(刪除、添加)

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView

editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

3.編輯狀態(tài)進(jìn)行提交

-(void) tableView:(UITableView *) tableViewcommitEditingStyle:

(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:

(NSIndexPath *)indexPath

UITableViewController

一:UITableViewController繼承自UIViewController,自帶

一個(gè)tableView

二:self.view不是UIView而是UITableView

三:datasource和delegate默認(rèn)都是

self(UITableViewController)

四:開(kāi)發(fā)中只需要建立UITableViewController子類

總結(jié)

1.UITableView編輯的寫(xiě)法

2.UITableViewController

第十二節(jié)

自定義Cell

為什么需要自定義Cell?

? 在前期我們學(xué)過(guò)自定義視圖,即創(chuàng)建一個(gè)類繼承于UIView

或者其他的視圖,在自定義類中創(chuàng)建其子視圖,這樣就會(huì)

形成一個(gè)新的自定義視圖。

? 系統(tǒng)提供的cell滿足不了復(fù)雜的樣式,因此:自定義Cell

和自定義視圖?一樣,自己創(chuàng)建一種符合我們需求的Cell并

使用這個(gè)Cell

?自定義Cell步驟:

? 創(chuàng)建?一個(gè)類繼承于UITableViewCell。

? 實(shí)現(xiàn)UITableViewCell的初始化?方法:

-(instancetype)initWithStyle:(UITableViewCellStyle)style

reuseIdentifier:(NSString *)reuseIdentifier。

? 確保所有的你想添加的子視圖都在自定義Cell的初始化方法中創(chuàng)建,由于UITableView的重用機(jī)制,一個(gè)Cell在第一次創(chuàng)建成功并用于下一次顯示的時(shí)候,不會(huì)再走初始化方法,這樣

可以避免子視圖的重復(fù)創(chuàng)建。

? 在Cell的子視圖創(chuàng)建成功后,將子視圖設(shè)置為屬性,類似于

UITableViewCell所自帶的textLabel和detailTextLabel屬性。便于在UITableView的協(xié)議中給自定義視圖賦值。

Model的使用

創(chuàng)建步驟:

? 創(chuàng)建一個(gè)類并繼承于NSObject

? 添加和字典中對(duì)應(yīng)的屬性

? 在視圖控制器中將字典通過(guò)KVC為Model賦值

? 將Model對(duì)象添加到數(shù)組中并刷新TableView

判斷多種Cell一

Model *model = [self.tableArray

objectAtIndex:indexPath.row];

//根據(jù)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;

}

圖片自適應(yīng)高度

UIImage *aImage = [UIImageimageNamed:@"1.png"];

//獲得圖片真實(shí)高度和寬度

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)];

總結(jié)

? 自定義Cell

?Model類的創(chuàng)建和使用

? 多種Cell混合使用

?Cell的自適應(yīng)高度

第十三節(jié)

視圖控制器獨(dú)有初始化方法:

- (instancetype)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

?nibNameOrNil:當(dāng)前控制器相關(guān)聯(lián)的nib文件,如果寫(xiě)nil默認(rèn)為同名文件

?nibBundleOrNil:當(dāng)前應(yīng)用程序包所在文件,如果寫(xiě)nil默認(rèn)為mainBundle

?在我們使?用init初始化(或者new直接創(chuàng)建)控制器的時(shí)候,會(huì)自動(dòng)執(zhí)行

控制器的此方法,參數(shù)均以默認(rèn)值執(zhí)行,所以可不寫(xiě)。

自動(dòng)布局

自動(dòng)布局:autoLayout,不給View固定的位置,通過(guò)某些規(guī)則讓View自己適應(yīng)自己的位置。

iOS6.0之后推出,在iOS8.0自動(dòng)布局被大幅度優(yōu)化,iOS9中新增了許多功能。

總結(jié)

1.xib可視化編程方式。

2.基礎(chǔ)控件的屬性操作以及與代碼的關(guān)聯(lián)方式。

3.自定義單元格的創(chuàng)建和使用。

4.自動(dòng)布局的使用。

第十四節(jié)

StoryBoard注意事項(xiàng)

? 在AppDelegate的

-application: didFinishLaunchingWithOptions:

方法中不要再用代碼初始化一個(gè)window。

? 將創(chuàng)建好的Storyboard在應(yīng)?用程序配置General中設(shè)置為MainInterface。

? 視圖添加與控制和IB開(kāi)發(fā)一樣。

利用StoryBoard繪制?自定義單元格

StoryBoard繪制單元格的時(shí)候要注意以下幾點(diǎn):

1.創(chuàng)建?自定義cell時(shí)選中左側(cè)TableViewCell。

2.繪制自定義UI界?面。

3.設(shè)置重用標(biāo)識(shí)符。

4.將StoryBoard文件關(guān)聯(lián)至對(duì)應(yīng)的UITableViewController和

UITableViewCell子類(自己創(chuàng)建的類)。

5.在UITableViewController中完成代碼書(shū)寫(xiě):設(shè)置section和row數(shù)量,

設(shè)置cell,根據(jù)實(shí)際情況調(diào)整cell的高度。

注意:cell不再需要注冊(cè)。

StoryBoard進(jìn)行頁(yè)面跳轉(zhuǎn)

StoryBoard頁(yè)面跳轉(zhuǎn)分為兩種。

?1、代碼方式:使用代碼通過(guò)控制器標(biāo)識(shí)來(lái)跳轉(zhuǎn)。比如在當(dāng)前頁(yè)

面的某一個(gè)事件中跳轉(zhuǎn)到一個(gè)標(biāo)識(shí)為“customVC”的控制器頁(yè)面

中:[self performSegueWithIdentifier:@"customVC"sender:nil];

?2、連線方式:直接使用拖拽可以給按鈕連線關(guān)聯(lián)兩個(gè)頁(yè)面:選中按鈕,按住control,從按鈕向下一級(jí)頁(yè)面連線。按鈕不需要添加響應(yīng)方法。

自定義segue

?步驟一:新建一個(gè)類繼承自UIStoryboardSegue。

?步驟二:選中前一個(gè)控制器,按住control鼠標(biāo)輔助完成連線,選擇custom。

?步驟三:選中自定義segue,設(shè)置segue的identifier以及關(guān)聯(lián)類。

?步驟四:在segue類里面重寫(xiě)perform方法(界面間跳轉(zhuǎn)默認(rèn)執(zhí)行的方法),自定義跳轉(zhuǎn)效果。

@implementation CustomSegue

//頁(yè)面跳轉(zhuǎn)時(shí),自定義segue會(huì)自動(dòng)觸發(fā)此方法(重寫(xiě)的系統(tǒng)方法)

-(void)perform

{

//獲取源控制器

UIViewController *v1 = (UIViewController

*)self.sourceViewController;

//獲取目標(biāo)控制器

UIViewController *v2 = (UIViewController

*)self.destinationViewController;

//自定義頁(yè)面切換效果

[UIView transitionFromView:v1.view toView:v2.viewduration:

10 options:UIViewAnimationOptionTransitionCurlDown

completion:^(BOOL finished) {

//動(dòng)畫(huà)完成后執(zhí)行的部分

}];

}

sizeClasses

?設(shè)備對(duì)應(yīng)關(guān)系如下:

?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)

總結(jié)

1.StoryBoard與xib在可視化編程中的對(duì)比。

2.可視化編程中頁(yè)?面?zhèn)髦档姆绞健?/p>

3.自定義segue實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)動(dòng)畫(huà)。

4.sizeClass適配。

第十五節(jié)

創(chuàng)建UICollectionViewFlowLayout

UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];

//設(shè)置每個(gè)item的大小

flowLayout.itemSize = CGSizeMake(100, 100);

//設(shè)置每個(gè)item的最小列間距(默認(rèn)是10)

flowLayout.minimumInteritemSpacing = 10;

//設(shè)置每個(gè)item的最小行間距(默認(rèn)是10)

flowLayout.minimumLineSpacing = 10;

//設(shè)置分區(qū)間隔 (上,左,下,右)

flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10,10);

//設(shè)置UICollectionView的滑動(dòng)方向

flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;

//頭部引用的尺寸

flowLayout.headerReferenceSize = CGSizeMake(100, 100);

//尾部引?用的尺寸

flowLayout.footerReferenceSize = CGSizeMake(100, 100);

設(shè)置代理

遵守代理協(xié)議:

@interface ViewController()

UICollectionViewDelegate>

@end

@implementation ViewController

指定代理人:

collectionView.delegate = self;

collectionView.dataSource = self;

創(chuàng)建item視圖對(duì)象

//創(chuàng)建item視圖對(duì)象

-(UICollectionViewCell *)collectionView: (UICollectionView*)collectionView

cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

UICollectionViewCell *cell = [collectionView

dequeueReusableCellWithReuseIdentifier:@"collectionCELL"forIndexPath:indexPath];

cell.backgroundColor = [UIColor redColor];

return cell;

}

返回頭部、尾部視圖樣式

UICollectionView不能像UITableView一樣直接指定頭部和尾部視

圖,需要注冊(cè)使用,最大的好處是添加了重用機(jī)制。

//注冊(cè)頭部視圖

[collectionView registerClass:[UICollectionReusableViewclass]

forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerView"];

//注冊(cè)尾部視圖

[collectionViewregisterClass:[UICollectionReusableView class]

forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footerView"];

第十六節(jié)

單例模式

? 單例模式是一種設(shè)計(jì)模式

? 實(shí)現(xiàn): 定義一個(gè)單例類,保證程序中這個(gè)類只能創(chuàng)建唯

一的實(shí)例對(duì)象,實(shí)現(xiàn)資源共享

單例代碼展示

static DataHandle *handle = nil;

//單例類使用此方法,創(chuàng)建單例對(duì)象

+ (DataHandle *)shareInstance

{

if (nil == handle) {

//如果還沒(méi)有創(chuàng)建過(guò)對(duì)象,使用handle指向新創(chuàng)建的對(duì)象

handle = [[DataHandle alloc] init];

}

//如果已經(jīng)創(chuàng)建過(guò)對(duì)象,則直接返回已經(jīng)創(chuàng)建的對(duì)象

return handle;

}

注意事項(xiàng)

?1)操作單例對(duì)象的變量存儲(chǔ)在靜態(tài)區(qū)程序關(guān)閉后由系統(tǒng)自動(dòng)

回收。

?2)單例對(duì)象存儲(chǔ)在堆區(qū),不需要釋放程序,關(guān)閉后由系統(tǒng)自

動(dòng)回收。

?3)變量和單例對(duì)象的生命周期與程序同步。

優(yōu)勢(shì)

?1)在內(nèi)存中只有一個(gè)對(duì)象,節(jié)省內(nèi)存空間

?2)避免頻繁的創(chuàng)建銷毀對(duì)象,可以提高性能

?3)避免對(duì)共享資源的多重占用

?4)可以全局訪問(wèn)

?5)降低模塊之間的耦合度,降低代碼的復(fù)雜度

框架設(shè)置

項(xiàng)目頁(yè)面框架步驟:

1、根據(jù)項(xiàng)目的頁(yè)面?zhèn)€數(shù)創(chuàng)建相同個(gè)數(shù)的視圖控制器

2、根據(jù)項(xiàng)目的頁(yè)面邏輯創(chuàng)建每個(gè)視圖控制器上的切換方法

3、根據(jù)項(xiàng)目的頁(yè)面功能創(chuàng)建每個(gè)視圖控制器上的功能方法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,750評(píng)論 1 14
  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,307評(píng)論 3 38
  • 標(biāo)題送給我關(guān)注了有一段時(shí)間的公眾號(hào)主人。 11.03 那天心情本來(lái)很好,陽(yáng)光也很好,下午還在辦公室和同事說(shuō)說(shuō)笑笑,...
    良人將至閱讀 702評(píng)論 0 3
  • 2. 自學(xué)該怎么學(xué)?有沒(méi)有入門書(shū)籍推薦? 首先,對(duì)于一個(gè)新手來(lái)說(shuō),最好的學(xué)習(xí)方法應(yīng)該是系統(tǒng)學(xué)習(xí)(我覺(jué)得無(wú)論哪科知識(shí)...
    laozhao閱讀 1,226評(píng)論 0 0
  • 正確的結(jié)果,是從大量錯(cuò)誤中得出來(lái)的;沒(méi)有大量錯(cuò)誤作臺(tái)階,也就登不上最后正確結(jié)果的高座。 ——錢學(xué)森 今天總結(jié)的主題...
    娃娃菜大五閱讀 294評(píng)論 0 1

友情鏈接更多精彩內(nèi)容