UI控件及方法大集合

廢話不多說,直接上干貨


---------------------------------------------------------------------

(1)

一、main.m

1、main函數,程序唯一的入口

2、UIApplicationMain函數函數作用

1)創建一個應用程序UIApplication對象,他是應用程序的象征,一個UIApplication對象就代表一個應用程序

2)指定誰管理應用程序的生命周期

2)建立一個事件循環來捕捉處理用戶的行為

3、永遠不用嘗試改變main.m中的內容

二、程序的生命周期

1、UIApplication對象實例化后,程序啟動時首先會調用該方法

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

2、當應用程序將要進入非活動狀態執行,比如來電話了、鎖屏等。

- (void)applicationWillResignActive:(UIApplication *)application

3、當應用程序進入活動狀態執行,這個剛好跟上面那個方法相反

- (void)applicationDidBecomeActive:(UIApplication *)application

4、一般程序進入后臺,就會進入非活躍狀態,但如果你的程序支持后臺,程序在后臺也保持活躍狀態

- (void)applicationDidEnterBackground:(UIApplication *)application

5、程序進入前臺

- (void)applicationWillEnterForeground:(UIApplication *)application

6、當程序將要退出是被調用,通常是用來保存數據和一些退出前的清理工作

- (void)applicationWillTerminate:(UIApplication *)application

7、找到info.plist文件,添加Application does not run in background,并設置YES,使應用不支持后臺操作,一旦退到后臺程序就退出

8、練習

驗證應用程序的聲明周期

三、UIWindow

1、在iOS中,使用窗口與試圖在屏幕上顯示應用程序的內容,窗口本身不具有任何可見的內容,但他對于應用程序的試圖提供一個基本的容器,試圖定義你想要的一些內容,例如:圖像、文本、表格等等

2、程序中每個時刻只能有一個UIWindow是keyWindow,通常應用程序只有一個window

3、iOS程序啟動完畢后,會首先創建一個UIWindow

4、一個iOS程序之所以能顯示到屏幕上,完全是因為它有UIWindow,也就是說沒有UIWindow,就看不見任何UI界面

5、常用方法

1)讓當前UIWindow變成keyWindow(主窗口)

- (void)makeKeyWindow;

2)讓主窗口顯示出來

- (void)makeKeyAndVisible;

6、練習

自己創建一個window作為主窗口

四、程序的完整啟動過程

1、main函數

2、UIApplicationMain

1)創建UIApplication對象

2)創建UIApplication的delegate對象

3、delegate對象開始處理(監聽)系統事件

1)程序啟動完畢的時候, 就會調用代理的application:didFinishLaunchingWithOptions:方法

2)在application:didFinishLaunchingWithOptions:中創建UIWindow

3)創建和設置UIWindow的rootViewController

4)顯示窗口

五、手機屏幕的幾個概念

1、UIScreen

可以充當iOS物理屏幕的替代者,

[UIScreen mainScreen] bounds] 能夠獲取屏幕大小

2、屏幕尺寸

指具體的屏幕物理長度,以屏幕的對角線的長度作為試試

3、像素

圖像由一個個點組成,這個點叫做像素

4、屏幕分辨率

指屏幕屏幕上總共的物理像素點

5、屏幕尺寸

設備? ? ? 屏幕尺寸? 分辨率(pt) reader 分辨率(px)

3GS? ? ? 3.5? ? ? 320*480? @1x? ? 320*480

4/4S? ? ? 3.5? ? ? 320*480? @2x? ? 640*960

5/5S/5C? 4.0? ? ? 320*568? @2x? ? 750*134

6? ? ? ? 4.7? ? ? 375*667? @2x? ? 750*1334

6P? ? ? ? 5.5? ? ? 414*736? @3x? ? 1242*2208

pt: 用于計算屏幕上的坐標

六、iOS坐標系統

1、iPhone的試圖坐標系是以左上角為原點

2、每一個view的frame所使用的坐標系以它的父試圖的左上角為原點

3、試圖結構和相關函數

//試圖顯示的位置

CGPoint point = CGPointMake(x,y)

//試圖顯示的大小

CGSize size = CGSizeMake(width,height)

//試圖顯示的位置和大小

CGRect rect = CGRectMake(x,y,width,height);

4、frame/Bounds/center

frame: 包含試圖的位置和大小

Bounds:包含試圖的大小,位置默認是(0,0)

center:包含試圖中心點所在的位置


(2)


一、UIViwe基本概念

1、iPhone上看到的控件大部分都是UIView的子類

2、UIView的三個作用

布局、動畫、事件傳遞

二、UIView的常用方法

UIKit是一個提供了在iOS上實現圖形,事件驅動程序的框架,能看到的到的試圖都在UIKit框架中

UIView是視圖的基類

1、 基本的添加和刪除

// 添加子視圖

addSubview:

// 視圖插入到指定索引位置

insertSubview:atIndex:

// 視圖插入指定視圖之上

insertSubview:aboveSubview:

// 視圖插入指定視圖之下

insertSubview:belowSubview:

// 把視圖移動到最頂層

bringSubviewToFront:

// 把視圖移動到最底層

sendSubviewToBack:

//把兩個索引對應的視圖調換位置

exchangeSubviewAtIndex:withSubviewAtIndex

// 把視圖從父視圖中移除

removeFromSuperview

2、查找試圖

viewWithTag

subViews

3、常用屬性

alpha? ? ? ? ? ? ? ? ? 透明度

backgroundColor? ? ? ? 背景顏色

subViews? ? ? ? ? ? ? 子視圖集合

hidden? ? ? ? ? ? ? ? 是否隱藏

//標簽值

tag

//父視圖

superview

//是否響應觸摸事件

userInteractionEnabled

4、坐標系統變換

在OC中,通過transform屬性可以修改對象的平移、縮放比例和旋轉角度

1、CGAffineTransformScale? ? 對視圖比例縮放

2、CGAffineTransformRotate? 對視圖做變焦旋轉

3、CGAffineTransformTranslate 對視圖在原來的位置上做平移

5、動畫

開始動畫,將改變屬性的代碼放在開始和提交之前

[UIView beginAnimations:nil context:nil]

提交動畫

[UIView commitAnimations]之間

當動畫即將開始時執行

+ (void)setAnimationWillStartSelector:(SEL)selector? 執行delegate對象的selector

當動畫結束時執行

+(void)setAnimationDidStopSelector:(SEL)selector

動畫的持續時間,秒為單位

+ (void)setAnimationDuration:(NSTimeInterval)duration

動畫延遲delay秒后再開始

+ (void)setAnimationDelay:(NSTimeInterval)delay

動畫的開始時間,默認為now

+ (void)setAnimationStartDate:(NSDate *)startDate

動畫的重復次數

+ (void)setAnimationRepeatCount:(float)repeatCount

如果設置為YES,代表動畫每次重復執行的效果會跟上一次相反

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses


(3)


一、UIViewController的基本概念

1、見名知意,試圖控制器

3、每個試圖控制器都自帶一個試圖,并且負責這個試圖相關的一切事務

4、對于iOS應用程序來說,每一個頁面都是都是一個控制器,通過切換不同的控制器,來實現切換不同的界面

4、在MVC體系中負責Model與View的通信

5、檢測設備旋轉以及內存警告

想要App支持某個方向,需要兩個設置:

1) Deployment Info 中,勾選 Device Orientation

2) 在控制器中,重寫 supportedInterfaceOrientations 方法。(如不重寫,IPad 默認是UIInterfaceorientationMaskAll , IPhone 默認是 UIInterfaceOrientationMaskAllButUpsideDown )

模擬器模擬內存警告

選中模擬器->hardware->simulate memory warning

6、UIViewController是所有試圖控制器類的基類,定義了試圖控制器的基本功能

二、UIViewController的生命周期

0、每次訪問控制器的view,view是nil的話就會自動調用該方法,如果重寫在這個方法內必須給控制器設置一個根試圖,如果不給的話程序就會無限循環直到程序奔潰

loadView

1、控制器的View加載完畢

viewDidLoad

2、View即將顯示到window上

viewWillAppear

3、view顯示完畢

viewDidAppear

4、View即將從window上移除

viewWillDisappear

5、view從window上移除完畢

viewDidDisappear

6、view即將銷毀的時候調用

viewWillUnload

7、view銷毀完畢的時候調用

viewDidUnload

8、當接收到內存警告的時候

didReceiveMemoryWarning

9、具體流程查看課件中的圖片

五、模態試圖的使用

1、模態視圖不是專門的某個類,而是通過視圖控制器的presentModalViewController:方法彈出的視圖我們都稱為模態視圖

2、模態視圖出現的場景一般是臨時彈出的窗口,譬如:登陸窗口

3、通過設置將要彈出控制器的modalTransitionStyle屬性設置不同的動畫效果

3、彈出,

presentModalViewController:

4、取消:

dismissModalViewControllerAnimated: 方法關閉窗口

UIControl的事件

1、UIControlEventTouchDown

單點觸摸按下事件:用戶點觸屏幕,或者又有新手指落下的時候。

2、UIControlEventTouchDownRepeat

多點觸摸按下事件,點觸計數大于1:用戶按下第二、三、或第四根手指的時候。

3、UIControlEventTouchDragInside

當一次觸摸在控件窗口內拖動時。

4、當一次觸摸在控件窗口之外拖動時

UIControlEventTouchDragOutside

5、當一次觸摸從控件窗口之外拖動到內部時

UIControlEventTouchDragEnter

6、當一次觸摸從控件窗口內部拖動到外部時

UIControlEventTouchDragExit

7、所有在控件之內觸摸抬起事件

UIControlEventTouchUpInside

9、點擊手指抬起時

UIControlEventTouchUpInside

10、所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。

UIControlEventTouchCancel


(3)

一、UINavigationController(導航控制器)

1、基本概念

1)繼承于UIViewControler

2)相當于一個容器用來管理有層級關系的控制器

3)采用棧的方式管理所有controller,每個controller管理各自的試圖。

棧:

向棧中添加一個對象的操作稱為入棧

在棧中刪除一個對象的操作稱為出棧

第一個入棧的對象叫做基棧

最后一個入棧的對象,叫做棧頂

當前顯示的試圖控制器,即為棧頂。

對象出棧、入棧的方式:后進先出,先進后出

4)提供返回上一級controller的默認button和方法

5)創建時要給導航控制器設置根控制器

2、結構,包含三個部分

1)navigationBar

2) 內容試圖

3)toolBar,因為不常用,默認是隱藏的

3、常用屬性和方法

屬性

1)獲取到在棧中最頂層的試圖控制器

topViewController

2)獲取到在棧中當前顯示的試圖控制器

visibleViewController

3)在棧中當前有的試圖控制器

viewControllers

4)隱藏導航欄

navigationBarHidden

5)獲取到導航欄

navigationBar

方法

1)初始化一個根視圖控制器,在棧的最底層

initWithRootViewController:(UIViewController *)rootViewController;

2)往棧中壓入一個新的控制器

pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

3)移除棧頂的一個控制器

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

4)彈出到指定的視圖控制器中,返回的數組代表要出棧中移除的對象

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

5)回到根視圖控制器

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

二、練習

在第二個視圖控制器在添加一個UIButton實例,單擊該按鈕時,壓入一個新的視圖控制器。在該視圖中添加四個按鈕,依次實現,

1、壓入一個新的視圖控制器;

2、返回上一個視圖控制器;

3、返回根視圖控制器;

4、返回指定的視圖控制器

三、UINavigationBar(導航欄)

1、基本概念

1)和導航控制器一樣,是一個容器,用來顯示導航欄上的試圖,

2) 豎屏44 橫屏32

2、配置外觀

1) 設置導航欄樣式

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

2)設置導航欄是否透明,設為不透明能夠屏蔽導航欄對試圖frame的影響

self.navigationController.navigationBar.translucent = NO;

1) 配置背景顏色

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

2)配置背景圖片

self.navigationController.navigationBar setBackgroundImage: forBarMetrics:

3) 改變導航欄上默認按鈕顏色

self.navigationBar.tintColor

3、配置內容

1)每個controller都有專屬的navigationItem

1)通過配置不同控制器的navigationItem讓不同控制器的導航欄顯示不同的內容

2)navigationItem的常用屬性

leftBarButtonItem

rightBarButtonItem

backBarButtonItem

title

titleView

3) backBarButtonItem是由上一層控制器控制的

4) 導航欄的prompt屬性,通過navigationItem來設置,其主要作用是用于提示用戶。比如,用戶正在請求網絡數據時,提示用戶數據正在加載。待加載完成后可以將它的值設置為nil,取消顯示。

四、練習

將自己項目的注冊按鈕放到右上角,修改系統自帶的返回按鈕


(4)


一、分欄控制器的基本概念

1、UITabBarController和UINavigationController一樣是用來管理試圖控制器的

2、與導航控制器不同,tab控制器使用數組管理子試圖控制器的,并且子試圖之間是平等關系,導航控制器所管理的試圖控制器之間上

練習:

1、創建若干個子視圖控制器(它們是并列的關系)

2、創建一個數組,將已創建的子視圖控制器,添加到數組中

3、創建UITabBarController實例

4、tabBarController.viewControllers = viewControllers;

5、添加到window的rootViewController中

二、分欄試圖控制器的結構

1、有兩部分組成

1)contentView:顯示當前controller的view

2) tabBar(標簽欄):負責切換顯示controller, 高度為49

2、tabBar

1) 標簽欄是唯一的,就好比導航控制器的導航欄

2) 設置分欄的顏色

barTintColor

3) 設置分欄的背景圖片

backgroundImage

4)標簽欄的顯示與隱藏

hidden

5)設置導航控制器默認顯示的控制器

selectedIndex

3、tabBarItem

1) 用來控制一組控制器的切換,類似選項卡,每個Tab控制一個試圖控制器,點擊哪個tab就顯示對應的試圖控制器,當前的試圖控制器

2) 每個tabBarItem都可以設置title、image/selectedImages、badgeValue

3) 設置選中的顏色

分欄控制器.tabBar.tintColor

3) TabBar只能顯示五個tab Item,如果超過五個則會自動生成個Morede 標簽顯示剩余的Tab,這些Tab可以通過編輯顯示在UITabBar上

4) 自定義Item

[UITabBarItem alloc]initWithTitle: image: tag:

[UITabBarItem alloc]initWithTabBarSystemItem:tag:

3、支持國際化

1)找到plist文件

2)右鍵? add row

3)Localizations? 默認就一個ENGLISH

4)在 添加一個 Item 1? Chinese (simplified)

練習:

1、初始化我們需要在tabBarController中顯示的視圖控制器

2、初始化UItabBarItem

3、在子視圖控制器中添加UItabBarItem

4、我們將子視圖控制器放入數組中

5、初始化tabBarcontroller

6、將數組放入tabBar控制器中,方法viewControllers

三、代理監聽分欄控制器的切換

1、視圖將要切換時調用,viewController為將要顯示的控制器,如果返回的值為NO,則無法點擊其它分欄了

- (BOOL)tabBarController:(UITabBarController *)tabBarControllershouldSelectViewController:(UIViewController *)viewController

2、視圖已經切換后調用,viewController 是已經顯示的控制器

- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController

3、將要開始自定義item的順序

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers

4、將要結束自定義item的順序

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed

5、結束自定義item的順序

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed

四、集成分欄控制器和導航控制器

1、在Tab Bar控制器中某一個Tab中使用Navigation控制器,這是最常見的用法,我們將在下面具體講解這個用法

2、在一個Navigation控制器控制下的某一個或某些控制器是Tab Bar控制器,這時對該Tab Bar控制器的壓入和彈出方法和普通視圖控制器一樣

五、總結

1、UINavigationController、UITabBarController和UIViewController通常都是組合出現的,一定要熟練使用

2、UINavigationController、UITabBarController都是UIViewController的子類,管理的都是UIViewContrller

3、UINavigationController可以嵌套UITabBarController

4、UITabBarController可以嵌套UINavigationController


(5)


一、表視圖的介紹

1、表視圖,是iOS中最重要的試圖,很多應用程序都會使用到,

2、表試圖里面可以放很多行信息

3、表視圖的兩種風格

1)普通風格

UITableViewStylePlain

2)分組風格

UITableViewStyleGrouped

3)UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實并沒有本質區別,只是后者按分組樣式顯示前者按照普通樣式顯示而已。

二、表視圖的基本結構

1、表視圖有表頭、表尾、中間一連串單元格試圖組成

1)設置表頭

tableHeaderView

2)設置單元格試圖

UITableViewCell,單元格也可以分段顯示,每一段都可以通過代理設置段頭和段尾

2)設置表尾

tableFooterView

3) tableView的常用屬性和方法

設置表視圖分割線風格

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;

設置表視圖分割線顏色,默認標準灰色

@property(nonatomic,retain) UIColor *separatorColor;

設置表視圖的頭部視圖

@property(nonatomic,retain) UIView *tableHeaderView;

設置表視圖的尾部視圖

@property(nonatomic,retain) UIView *tableFooterView;

設置表視圖單元格的行高

@property(nonatomic) CGFloat rowHeight;

設置表視圖背景

@property(nonatomic, readwrite, retain) UIView *backgroundView

刷新表視圖單元格中數據

- (void)reloadData;

顯示指示條

showsVerticalScrollIndicator

設置表視圖section頭部行高

@property(nonatomic) CGFloat sectionHeaderHeight;

設置表視圖section尾部部行高

@property(nonatomic) CGFloat sectionFooterHeight

三、單元格的顯示

1、單元格的位置表示

NSIndexPath:能表示當前cell是tableView的第幾段第幾行

2、單元格的創建

UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

cell的樣式

1)UITableViewCellStyleDefault

左側顯示textLabel,imageView顯示在最左邊

2)UITableViewCellStyleValue1

左側顯示textLabel、右側顯示detailTextLabel,imageView顯示在最左邊

3)UITableViewCellStyleValue2

左側依次顯示textLabel(默認藍色)和detailTextLabel,imageView可選

4)UITableViewCellStyleSubtitle

左上方顯示textLabel,左下方顯示detailTextLabel,imageView顯示在最左邊

cell的輔助圖標 accessoryType

1) 不顯示任何圖標

UITableViewCellAccessoryNone,

2) 跳轉指示圖標

UITableViewCellAccessoryDisclosureIndicator

3) 內容詳情圖標和跳轉指示圖標

UITableViewCellAccessoryDetailDisclosureButton

4) 勾選圖標

UITableViewCellAccessoryCheckmark

5) 內容詳情圖標

UITableViewCellAccessoryDetailButton

5) 自定義輔助圖標

accessoryView屬性

3、cell的常用屬性

1)設置cell的背景試圖

backgroundView

2)設置選中的cellbei的背景圖片

selectedBackgroundView

3) 設置選中時的樣式

selectionStyle

練習:不分組的名人錄

四、數據源方法(UITableViewDatasource)

1、實例化表視圖時,必須要實現他的數據源方法,以此來完成表中數據的配置,一般來說數據源方法是用來配置表中的數據

2、常用數據源方法

1)配置section中含有行數

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

2)創建單元格實例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3) 配置表視圖section個數,默認為1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

4)section中的頭部視圖的標題

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

5)section中的尾部視圖的標題

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

/* 表視圖的編輯 移動、刪除等 */

6)指定單元格是否支持編輯

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;

7)指定單元格是否支持移動

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;

8)用戶編輯了哪一個單元格,在這里執行刪除操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath;

9)實現此方法,移動單元格

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

五、代理方法(UITableViewDelegate)

1、一般是處理表視圖基本樣式(單元格高度)以及捕捉選中單元格事件

2、常用代理方法

1)配置行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

2)設置section 頭部、尾部視圖的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

3)自定義section頭部、尾部視圖,注意:需要指定高度

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

4)用戶單擊單元格中輔助按鈕時,調用該方法

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

5)用戶單擊單元格,調用該方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

6)取消選中單元格時,調用該方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

7)設置單元格編輯樣式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

練習:分組的名字錄

1、設置段頭、段尾

2、自定義段頭段尾

一、表視圖常用屬性和方法

屬性

1、設置表視圖分割線風格

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;

2、設置表視圖分割線顏色,默認標準灰色

@property(nonatomic,retain) UIColor *separatorColor;

3、設置表視圖的頭部視圖

@property(nonatomic,retain) UIView *tableHeaderView;

4、設置表視圖的尾部視圖

@property(nonatomic,retain) UIView *tableFooterView;

5、設置表視圖單元格的行高

@property(nonatomic) CGFloat rowHeight;

6、設置表視圖背景

@property(nonatomic, readwrite, retain) UIView *backgroundView

7、刷新表視圖單元格中數據

- (void)reloadData;

8、設置表視圖section頭部行高

@property(nonatomic) CGFloat sectionHeaderHeight;

9、設置表視圖section尾部部行高

@property(nonatomic) CGFloat sectionFooterHeight;

10、 刷新表視圖section中數據

- (void)reloadSectionIndexTitles

11、默認為NO,不可以編輯,設置時,不存在動畫效果

@property(nonatomic,getter=isEditing) BOOL editing;

12、覆蓋此方法,存在動畫效果

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

13、默認為YES,當表視圖不在編輯時,單元格是否可以選中

@property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);

14、默認為NO,當表視圖在編輯時,單元格是否可以選中

@property(nonatomic) BOOL allowsSelectionDuringEditing;

15、默認為NO,是否可以同時選中多個單元格,注意版本問題

@property(nonatomic) BOOL allowsMultipleSelection

17、 默認為NO,在編輯狀態下時,是否可以同時選中多個單元格,注意版本問題

@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

方法

1、指定一個cell,返回一個NSIndexPath實例,如果cell沒有顯示,返回nil

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

2、指定一個范圍,返回一個數組,內容是NSIndexPath實例,指定rect無效,返回nil

- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;

3、指定一個NSIndexPath,返回一個cell實例,如果cell沒有顯示,返回為nil

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

4、根據顯示的cell,返回一組cell實例的數組,如果沒有顯示,返回nil

- (NSArray *)visibleCells;

5、根據顯示的cell,返回一組NSIndexPath實例的數組,如果沒有顯示,返回nil

- (NSArray *)indexPathsForVisibleRows;

6、滑動到指定的位置,可以配置動畫

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

7、插入一行cell,指定一個實現動畫效果

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

8、刪除一行cell, 指定一個實現動畫效果

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

9、刷新一個行cell,指定一個實現動畫效果

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

10、移動cell的位置,指定一個實現動畫效果

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath NS_AVAILABLE_IOS(5_0);


(6)


-、表視圖的編輯狀態

1、表視圖的編輯狀態有兩種

insert和delete

2、實現表視圖編輯的步驟

1)讓tableview處于編輯狀態

self.tableView.editing

2)通過代理方法確定tableView處于哪種狀態(添加還是刪除)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

3)選擇添加或者刪除通過代理方法來做不同的處理

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

二、單元格的移動

只需實現兩個代理方法

1、實現代理方法,讓tableView的單元格支持移動,如果該方法返回為NO,則不支持單元格的移動,該方法一般可省略

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

2、實現代理方法,指定從哪里移到哪里

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

一、謂詞的基本概念

cocoa中提供了NSPredicate類,指定過濾器的條件,將符合條件的對象保留下來

二、創建謂詞的步驟

1、設置謂詞條件

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"];

2、使用謂詞

1) 判斷當前對象是滿足條件

[predicate evaluateWithObject:id]

2) 將數組中滿足條件的內容翻到數組中并返回該數組

[array filteredArrayUsingPredicate:predicate];

三、運算符,謂詞中的字符串用單引號括起來

1、邏輯運算符 &&(AND) ||(OR)

[NSPredicate predicateWithFormat:@"age<25 || age>27"];

2、根據關鍵字查詢 IN

[NSPredicate predicateWithFormat:@"name in {'tom-8','jack-3','xxx'}"];

[NSPredicate predicateWithFormat:@"name in %@",inArray];

3、檢查某個字是否以**開頭? BEGINSWITH

[NSPredicate predicateWithFormat:@"name BEGINSWITH 't' "];

4、檢查某個單詞是否已**結尾 ENDSWITH

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 't' "];

5、是否包含某個字符 CONTAINS

[NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];

6、檢查包含某個字符(模糊查詢) Like,?和*可作為通配符,其中?匹配1個字符,*匹配0個或者多個字符

[NSPredicate predicateWithFormat:@"name like '??c*'"];


(7)


一、事件

1、在iOS上,事件有多種形式

1)觸摸事件

2)運動事件

3)遠程控制事件

2、UIView不接收觸摸事件的三種情況

1.不接收用戶交互

userInteractionEnabled = NO

2.隱藏

hidden = YES

3.透明

alpha = 0.0 ~ 0.01

提示:UIImageView的userInteractionEnabled默認就是NO,因此UIImageView以及它的子控件默認是不能接收觸摸事件的

二、事件處理基本方法

1、一個或多個手指觸碰屏幕

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

2、一個或多個手指在屏幕上移動

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

3、一個或多個手指離開屏幕

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

4、觸摸序列被諸如電話呼入這樣的系統事件取消

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

三、UITouch觸摸對象

當用戶觸摸屏幕時,事件會被封裝成一個event實例,包含了用戶觸摸的相關信息,event實例中包含著若干個UITouch實例,一個touch代表著用戶的一個手指

1、UITouch常用屬性

1)window

觸摸產生時所處的窗口

2)view

觸摸產生時所處的試圖

3)tapCount

tap(輕擊)操作,和鼠標單獨單擊操作類似,tapCount表示短時間內輕擊屏幕的次數,因此可以根據tapCount判斷單擊、雙擊或更多的輕擊

雙擊試圖是時單擊也會執行的解決方法

if (touch.tapCount == 1) {

//延遲0.5秒執行 runLoop會計算這個時間

[self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];

}else{

//告訴runLoop取消調用某個延遲的方法

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];

[self doubleTap];

}

4)timestamp

記錄了觸摸事件產生或變化時的時間,單位是秒

5)phase

觸摸事件在屏幕上有一個周期,即觸摸開始、觸摸點移動、觸摸點結束,還有中途取消,通過phase可以查看當前觸摸事件在一個周期中所處的狀態,pase是一個枚舉,包含:

//觸摸開始

UITouchPhaseBegan

//接觸點移動

UITouchPhaseMoved

//接觸點無移動

UITouchPhaseStationary

//觸摸結束

UITouchPhaseEnded

//觸摸取消

UITouchPhaseCancelled

2、UITouch常用方法

1)返回一個CGPoint類型的值,表示觸摸在view這個視圖上的位置,這里返回的位置是針對view的坐標系的

- (CGPoint)locationInView:(UIView *)view

2)該方法記錄了前一個坐標值

- (CGPoint)previousLocationInView:(UIView *)view:

一、事件傳遞

1、從事件發生到其處理的對象,傳遞要經過特殊的一段過程,當用戶點擊設備屏幕時,iOS捕捉到一系列的觸摸,將其打包到UIEvent對象并放置到應用程序活動事件隊列中

2、UIApplication對象從事件隊列中取出最前面的事件并將其分發,通常,其將事件發送給應用程序的主窗口-UIWindow實例,再由窗口對象發送事件給第一響應者處理,一般通過touchesBegan方法獲取該事件

3、具體過程

1)先將事件對象由上往下傳遞(由父控件傳遞給子控件),找到最合適的控件來處理這個事件

2)調用最合適控件的touches方法

3)如果調用了[super touches...]方法,就會將事件順著響應者鏈條往上傳遞,傳遞給上一個響應者

4)接著就會調用上一個響應者的touches...方法

二、響應者鏈

1、基本概念

響應者對象是一個能接受并處理事件的對象,UIResponser是所有響應者對象的基類,該基類定義了一系列編程接口,不但為事件處理進行服務而且還提供了通用的響應行為處理,UIApplication、UIView(UIWindow)、UIViewController都直接或間接的繼承自UIResponser,所有的這些類的實例都是響應者對象

響應者鏈表示一系列的響應者對象,事件被交由第一響應者對象處理,如果第一響應者不處理,事件被沿著響應者鏈向上傳遞,交給下一個響應者(nextresponder)

2、事件響應者鏈傳遞的過程

1、當用戶與試圖交互時,會將消息傳遞給試圖控制器,如果不存在控制器,傳遞給父試圖

2、如果不處理該消息,則繼續將消息向上傳遞,如果最上層的試圖也不處理,將事件交給window對象,最后交由UIApplication實例,如果不處理,丟棄事件

PS:傳遞方式

[self.nextResponder touchesBegan:touches withEvent:event];

3、通過響應者鏈傳遞可以讓多個試圖響應同一個手勢

三、練習

事件的傳遞演示

四、手勢識別器

UIGestureRecognizer類,用于檢測、識別用戶使用設備時所用的手勢,他是一個抽象類,定義了所有手勢的基本行為,以下是UIGestureRecognizer子類,用與處理具體的用戶手勢行為

1、輕擊

UITapGestureRecognizer

常用屬性

//點擊次數

numberOfTapsRequired

//消除兩個手勢的影響

requireGestureRecognizerToFail

//手指的數量,需將試圖的multipleTouchEnabled設為YES

numberOfTouchesRequired

2、輕掃

UISwipeGestureRecognizer

常用屬性

清掃方向,默認是右

direction

3、長按

UILongPressGestureRecognizer

常用屬性

最小長按時間

minimumPressDuration

注意事項

如果不判斷會調用2次 按下2秒后自己調用一次 松開后又要調用一次

if (longPress.state == UIGestureRecognizerStateEnded) {

return;

}

NSLog(@"長按");

4、平移

UIPanGestureRecognizer

常用方法

獲取觸摸的位置

locationInView

注意事項

- (void)panAction:(UIPanGestureRecognizer *)gesture{

// 1.在view上面挪動的距離 //translationInView表示相對于起點挪動的位置是最新2點之間的間距

CGPoint translation = [gesture translationInView:gesture.view];

CGPoint center = gesture.view.center;

center.x += translation.x;

center.y += translation.y;

gesture.view.center = center;

// 2.清空移動的距離

[gesture setTranslation:CGPointZero inView:gesture.view];

}

5、捏合

UIPinchGestureRecognizer

注意事項:

每次調用過方法后,記得將scale置1,否則你的圖片在捏合一點便會變很大或很小

6、旋轉

UIRotationGestureRecognizer

注意事項:

每次調用過方法后,記得將rotation置0,否則圖片旋轉不正常

7、手勢識別器的代理方法

/**

*? 是否允許多個手勢識別器同時有效

*? Simultaneously : 同時地

*/

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

8、重要的事情說三遍

實現旋轉、縮放、平移的時候一定要清零

實現旋轉、縮放、平移的時候一定要清零

實現旋轉、縮放、平移的時候一定要清零


(8)

一、滑動試圖(UIScrollView)

1、基本概念

1) 移動設備的屏幕大小是極其有限的,因此直接展示在用戶眼前的內容也相當有限,當展示的內容較多,超出一個屏幕時,用戶可通過滾動手勢來查看屏幕以外的內容

2) 通過scrollView可以通過手勢,放大或者縮小顯示的內容

2、UIScrollView常用屬性

1)里面內容的大小,也就是可以滾動的大小,模式是0,沒有滾動效果

contentSize

2)這個屬性能夠在UIScrollView的4周增加額外的滾動區域

contentInset

3) 這個屬性用來表示UIScrollView滾動的位置

contentOffset

4) 默認是 yes,就是滾動超過邊界會反彈有反彈回來的效果。假如是 NO,那么滾動到達邊界會立刻停止

bounces

5) 是否分頁

pagingEnabled

6) 是否可以滾動

scrollEnabled

7) 滾動時是否顯示水平滾動條

showsHorizontalScrollIndicator

8) 滾動時是否顯示垂直滾動條

showsVerticalScrollIndicator

9) 滾動條的樣式,基本只是設置顏色。總共3個顏色:默認、黑、白

indicatorStyle

10) 默認是 NO,可以在垂直和水平方向同時運動。當值是 YES 時,假如一開始是垂直或者是水平運動,那么接下來會鎖定另外一個方向的滾動。 假如一開始是對角方向滾動,則不會禁止某個方向

directionalLockEnabled

10) 讓指定的區域顯示顯示出來

scrollRectToVisible:animate

10)當touch后還沒有拖動的時候值是YES,否則是NO

tracking

11) 當內容放大到最大或者最小的時候是YES,否則NO

zoomBouncing

12) 當正在縮放的時候值是YES,否則為NO

zooming

13) 當滾動后,手指放開但是還在繼續滾動中。這個時候是 YES,其它時候是 NO

decelerating

14) 設置手指放開后的減速率

decelerationRate

15) 一個浮點數,表示能放最大的倍數

maximumZoomScale

16) 一個浮點數,表示能縮最小的倍數

minimumZoomScale

17) 和 bounces 類似,區別在于:這個效果反映在縮放上面,假如縮放超過最大縮放,那么會反彈效果;假如是 NO,則到達最大或者最小的時候立即停止

bouncesZoom

3、UIScrollView代理方法

創建一個UIScrollView,并在里面放置一張圖片

1)scrollView已經滑動

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;

2)視圖已經放大或縮小

- (void)scrollViewDidZoom:(UIScrollView *)scrollView;

3)scrollView開始拖動

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

4)scrollView結束拖動

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

5)scrollView開始減速(以下兩個方法注意與以上兩個方法加以區別)

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;

6)scrollview減速停止

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

用法:

if (![scrollView isMemberOfClass:[UITableView class]]) {

int current = scrollView.contentOffset.x / 320;

UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:101];

pageControl.currentPage = current;

}

7)返回一個放大或者縮小的視圖,要設置最大最小值

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;

8)開始放大或者縮小

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view

9)縮放結束時

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;

10)是否支持滑動至頂部

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;

11)滑動到頂部時調用該方法

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;

一、利用scrollView實現圖片的放大縮小

1、設置最大最小縮放倍率

self.maximumZoomScale = 2.5;

self.minimumZoomScale = 1;

2、指定縮放誰

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

3、雙擊事件的放大與縮小

- (void)zoomInOrOut:(UITapGestureRecognizer *)tapGesture

{

if (self.zoomScale >= 2.5) {

[self setZoomScale:1 animated:YES];

}else {

CGPoint point = [tapGesture locationInView:self];

[self zoomToRect:CGRectMake(point.x - 40, point.y - 40, 80, 80) animated:YES];

}

}

二、實現類似相冊的功能

1、創建一個scrollView,進行配置

2、在scrollView里面放多張能夠顯示得圖片

3、每次翻到下一張圖片時要讓這上個圖片恢復到原比例

int pre = 0;

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

int current = scrollView.contentOffset.x / 340;

ImageScrollView *imgScrollView = (ImageScrollView *)[scrollView viewWithTag:pre];

if (imgScrollView.zoomScale > 1) {

imgScrollView.zoomScale = 1;

}

pre = current;

}


(9)

一、UIPickerView(拾取器)的使用? 1、UIPickerView控件生成的表格可以提供滾動的輪盤,? 2、這些表格表面上類似于標準的UITableView控件,但是他們使用的dataSource和delegate方法有細微的差別二、UIPickerView常用方法? ? ? 1、獲取指定列的行數? ? - (NSInteger)numberOfRowsInComponent:(NSInteger)component;? 2、刷新所有列? ? - (void)reloadAllComponents;? 3、刷新指定的列? ? - (void)reloadComponent:(NSInteger)component;? 4、選擇一行? ? - (void)selectRow:(NSInteger)row inComponent:(NSInteger component? ? animated:(BOOL)animated;? 5、獲取指定列當前顯示的是第幾行? ? - (NSInteger)selectedRowInComponent:(NSInteger)component;三、UIPickerView常用方法? ? 1、dataSource方法? ? ? 1)返回列數? ? ? ? - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView? ? ? 2)返回每一列對應的行數? ? ? ? - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component? ? 2、delegate方法? ? ? ? 1)返回顯示的文本? ? ? ? - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component? ? ? ? 2)當某一列選中的行數發生變化時調用? ? ? ? ? - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component? ? ? ? 3) 設置行高? ? ? ? - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component? ? ? ? 4)設置列寬? ? ? ? - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component? ? ? ? 5)自定義cell? ? ? ? - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view四、練習? ? 1、使用拾取器做城市選擇? ? ? 1)在工程自帶的ViewController的viewDidload加入? ? ? ? UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-300, 414, 300)];? ? ? ? pickerView.backgroundColor = [UIColor grayColor];? ? ? ? ? ? [self.view addSubview:pickerView];? ? ? 運行,什么都沒有,因為多少段多少行是通過代理方法來設置的? ? ? 2)讓viewController類遵守兩個協議,在viewDidLoad方法里給pickerView掛上代理,并在viewController里實現如下兩個代理方法

//返回列數

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return 2;

}

//返回每一列中的行數

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

return 5;

}

運行 選擇器里面有兩列,每列的數據都是用『?』表示,因為還沒有給每列賦值

3)將課件里面的city.plist文件拖到工程中,在viewDidLoad方法里寫入如下代碼

NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];

//聲明一個全局變量dataArray

dataArray = [[NSArray alloc] initWithContentsOfFile:path];

4)將代理方法- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component的內容替換成如下內容

if (component == 0) {

return data.count;

}

NSInteger selectedRow =? [pickerView selectedRowInComponent:component];

NSArray *tempArray = data[selectedRow][@"cities"];

return tempArray.count;

運行 目前還是沒有數據,但列數和行數已經做了自動設置

5、給每行設置標題,將如下代理方法寫入工程

//返回每個item中的title

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (component == 0) {//第一列

NSDictionary *dic = data[row];

NSString *state = dic[@"state"];

return state;

}

//返回第一列選擇的行的索引

NSInteger selectedRow = [pickerView selectedRowInComponent:0];

//取得省級字典

NSDictionary *items = data[selectedRow];

//取得該省下所有的市

NSArray *cities = [items objectForKey:@"cities"];

NSString *cityName = cities[row];

return cityName;

}

運行 每行有標題 但左邊的省份變化,右邊的城市列表沒有跟著變化

6、實現右側跟著左側聯動

//當某一列選中的行數發生變化時調用

- (void)pickerView:(UIPickerView *)pickerView

didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

if (component == 0) {

//刷新指定列中的行

[pickerView reloadComponent:1];

//選擇指定的item

[pickerView selectRow:0 inComponent:1 animated:YES];

}

}

運行 右側可以跟著左側聯動

7、設置列寬和行高,在工程里添加如下代理方法

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

if (component == 0) {

return 100;

}

return 220;

}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

return 30;

}

8、自定義cell,添加代理方法

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

if (component == 0) {

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

view.backgroundColor = [UIColor greenColor];

return view;

}

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 40)];

view2.backgroundColor = [UIColor redColor];

return view2;

}

運行 只剩色塊,而里面卻沒有內容了,因為這個代理方法執行- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component代理方法將不會再執行

一、UIDatePicker時間拾取器

1、UIDatePicker提供了時間、日期供用戶選擇

2、UIDataPicker是對UIPickerView做了進一步封裝,其外觀布局和UIPickerView完全一樣

二、UIDatePicker的常用屬性

1、初始化顯示的date日期

date

2、設置最小日期

minimumDate

3、設置最大日期

maximumDate

4、設置日期的顯示樣式

datePickerMode

UIDatePickerModeTime 顯示時間

UIDatePickerModeDate 顯示日期

UIDatePickerModeDateAndTime 顯示日期和時間

UIDatePickerModeCountDownTimer 顯示時間

5、分鐘間隔值

minuteInterval

三、練習

1、日期選擇器

1) 新建工程,在ViewController中的viewDidLoad加入如下代碼

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 736-300, 414, 300)];

datePicker.tag = 100;

//最小時間? 10年之前 不設置最小時間將沒有限定

datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-60*60*24*365*10];

datePicker.maximumDate = [NSDate date];

//初始化時間為昨天

datePicker.date = [NSDate dateWithTimeIntervalSinceNow:-60*60*24];

//4種顯示樣式

datePicker.datePickerMode = UIDatePickerModeDate;

[self.view addSubview:datePicker];

運行 體驗一下設置最小時間跟沒設置最小時間的區別

2)在viewDidLoad中添加如下代碼

UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];

button.frame = CGRectMake(100, 100, 50, 50);

[button addTarget:self action:@selector(clickActon) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

3)實現點擊事件方法

- (void)clickActon{

UIDatePicker *datePicker = (UIDatePicker *)[self.view viewWithTag:100];

NSDate *date = datePicker.date;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSString *dateString = [dateFormatter stringFromDate:date];

NSLog(@"%@",dateString);

}

運行,查看效果

一、日期類簡單介紹

對日期我們經常使用到的兩個類

1、NSDate,NSDate的對象表示一個具體的時間點

2、NSDateFormatter對象將時間轉化為字符串或者反轉

二、創建NSDate對象的幾種方式以及區別

1、獲取到GTM時間(世界標準時間),比中國時間早八個小時

NSDate *data = [NSDate date];

2、從當前GTM時間往后推八個小時的時間,如果為負數就是往前推八個小時的時間

NSTimeInterval timeInterval = 8*60*60;

NSDate *chinaDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];

3、從計算機時間(1970-01-01 00:00:00)后推八個小時后的時間。

NSDate *since1970Date = [NSDate dateWithTimeIntervalSince1970:timeInterval];

4、從自定義的時間往后推八個小時后的時間。

NSDate *sinceCustomDate = [NSDate dateWithTimeInterval:timeInterval sinceDate:date];

5、從2001-01-01 00:00:00往后推八個小時后的時間。

NSDate *sinceReferenceDate = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval];

6、永遠不可能到達的一個點

NSDate *futureDate = [NSDate distantFuture];

7、一個無限過去的時間點

NSDate *pastDate = [NSDate distantPast];

三、NSDate對象常用的方法

1、從計算機時間(1970-01-01 00:00:00)到date時間的時間差(秒為單位)

timeIntervalSince1970

2、從(2001-01-01 00:00:00)到date時間的時間差(秒為單位)

timeIntervalSinceReferenceDate

3、從當前時間到date時間的時間差

timeIntervalSinceNow

4、當前時間偏移多少秒后的新時間

dateByAddingTimeInterval

5、兩個日期之間的時間差

timeIntervalSinceDate

6、日期的比較

earlierDate//誰早返回誰

laterDate//誰晚返回誰

isEqualToDate//兩個日期是否相等

四、日期與字符串的轉換

1、日期格式如下:

y? 年

F? 月份中的周數

E? 周幾,EEEE星期幾

M 表示 月

m 表示 分

H 表示 24小時制

h 表示 12小時制

s 表示 秒

S 表示 毫秒

d? 月份中的天數

a? Am/pm

k? 一天中的小時數(1-24)

K? am/pm 中的小時數(0-11)

H? 一天中的小時數

h? am/pm 中的小時數(1-12)

2、字符串與日期的轉換

1)將日期轉換為字符串,轉換過后就自動換成系統所在時區的時間

[dateFormatter stringFromDate:date]

2)將字符串轉化為日期

[dateFormatter dateFromString:str]

--------------------------------------------------------------------

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 未完(接UI控件及方法大集合續)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容