廢話不多說,直接上干貨
---------------------------------------------------------------------
(1)
一、main.m
1、main函數(shù),程序唯一的入口
2、UIApplicationMain函數(shù)函數(shù)作用
1)創(chuàng)建一個應(yīng)用程序UIApplication對象,他是應(yīng)用程序的象征,一個UIApplication對象就代表一個應(yīng)用程序
2)指定誰管理應(yīng)用程序的生命周期
2)建立一個事件循環(huán)來捕捉處理用戶的行為
3、永遠(yuǎn)不用嘗試改變main.m中的內(nèi)容
二、程序的生命周期
1、UIApplication對象實(shí)例化后,程序啟動時首先會調(diào)用該方法
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2、當(dāng)應(yīng)用程序?qū)⒁M(jìn)入非活動狀態(tài)執(zhí)行,比如來電話了、鎖屏等。
- (void)applicationWillResignActive:(UIApplication *)application
3、當(dāng)應(yīng)用程序進(jìn)入活動狀態(tài)執(zhí)行,這個剛好跟上面那個方法相反
- (void)applicationDidBecomeActive:(UIApplication *)application
4、一般程序進(jìn)入后臺,就會進(jìn)入非活躍狀態(tài),但如果你的程序支持后臺,程序在后臺也保持活躍狀態(tài)
- (void)applicationDidEnterBackground:(UIApplication *)application
5、程序進(jìn)入前臺
- (void)applicationWillEnterForeground:(UIApplication *)application
6、當(dāng)程序?qū)⒁顺鍪潜徽{(diào)用,通常是用來保存數(shù)據(jù)和一些退出前的清理工作
- (void)applicationWillTerminate:(UIApplication *)application
7、找到info.plist文件,添加Application does not run in background,并設(shè)置YES,使應(yīng)用不支持后臺操作,一旦退到后臺程序就退出
8、練習(xí)
驗(yàn)證應(yīng)用程序的聲明周期
三、UIWindow
1、在iOS中,使用窗口與試圖在屏幕上顯示應(yīng)用程序的內(nèi)容,窗口本身不具有任何可見的內(nèi)容,但他對于應(yīng)用程序的試圖提供一個基本的容器,試圖定義你想要的一些內(nèi)容,例如:圖像、文本、表格等等
2、程序中每個時刻只能有一個UIWindow是keyWindow,通常應(yīng)用程序只有一個window
3、iOS程序啟動完畢后,會首先創(chuàng)建一個UIWindow
4、一個iOS程序之所以能顯示到屏幕上,完全是因?yàn)樗蠻IWindow,也就是說沒有UIWindow,就看不見任何UI界面
5、常用方法
1)讓當(dāng)前UIWindow變成keyWindow(主窗口)
- (void)makeKeyWindow;
2)讓主窗口顯示出來
- (void)makeKeyAndVisible;
6、練習(xí)
自己創(chuàng)建一個window作為主窗口
四、程序的完整啟動過程
1、main函數(shù)
2、UIApplicationMain
1)創(chuàng)建UIApplication對象
2)創(chuàng)建UIApplication的delegate對象
3、delegate對象開始處理(監(jiān)聽)系統(tǒng)事件
1)程序啟動完畢的時候, 就會調(diào)用代理的application:didFinishLaunchingWithOptions:方法
2)在application:didFinishLaunchingWithOptions:中創(chuàng)建UIWindow
3)創(chuàng)建和設(shè)置UIWindow的rootViewController
4)顯示窗口
五、手機(jī)屏幕的幾個概念
1、UIScreen
可以充當(dāng)iOS物理屏幕的替代者,
[UIScreen mainScreen] bounds] 能夠獲取屏幕大小
2、屏幕尺寸
指具體的屏幕物理長度,以屏幕的對角線的長度作為試試
3、像素
圖像由一個個點(diǎn)組成,這個點(diǎn)叫做像素
4、屏幕分辨率
指屏幕屏幕上總共的物理像素點(diǎn)
5、屏幕尺寸
設(shè)備? ? ? 屏幕尺寸? 分辨率(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: 用于計算屏幕上的坐標(biāo)
六、iOS坐標(biāo)系統(tǒng)
1、iPhone的試圖坐標(biāo)系是以左上角為原點(diǎn)
2、每一個view的frame所使用的坐標(biāo)系以它的父試圖的左上角為原點(diǎn)
3、試圖結(jié)構(gòu)和相關(guān)函數(shù)
//試圖顯示的位置
CGPoint point = CGPointMake(x,y)
//試圖顯示的大小
CGSize size = CGSizeMake(width,height)
//試圖顯示的位置和大小
CGRect rect = CGRectMake(x,y,width,height);
4、frame/Bounds/center
frame: 包含試圖的位置和大小
Bounds:包含試圖的大小,位置默認(rèn)是(0,0)
center:包含試圖中心點(diǎn)所在的位置
(2)
一、UIViwe基本概念
1、iPhone上看到的控件大部分都是UIView的子類
2、UIView的三個作用
布局、動畫、事件傳遞
二、UIView的常用方法
UIKit是一個提供了在iOS上實(shí)現(xiàn)圖形,事件驅(qū)動程序的框架,能看到的到的試圖都在UIKit框架中
UIView是視圖的基類
1、 基本的添加和刪除
// 添加子視圖
addSubview:
// 視圖插入到指定索引位置
insertSubview:atIndex:
// 視圖插入指定視圖之上
insertSubview:aboveSubview:
// 視圖插入指定視圖之下
insertSubview:belowSubview:
// 把視圖移動到最頂層
bringSubviewToFront:
// 把視圖移動到最底層
sendSubviewToBack:
//把兩個索引對應(yīng)的視圖調(diào)換位置
exchangeSubviewAtIndex:withSubviewAtIndex
// 把視圖從父視圖中移除
removeFromSuperview
2、查找試圖
viewWithTag
subViews
3、常用屬性
alpha? ? ? ? ? ? ? ? ? 透明度
backgroundColor? ? ? ? 背景顏色
subViews? ? ? ? ? ? ? 子視圖集合
hidden? ? ? ? ? ? ? ? 是否隱藏
//標(biāo)簽值
tag
//父視圖
superview
//是否響應(yīng)觸摸事件
userInteractionEnabled
4、坐標(biāo)系統(tǒng)變換
在OC中,通過transform屬性可以修改對象的平移、縮放比例和旋轉(zhuǎn)角度
1、CGAffineTransformScale? ? 對視圖比例縮放
2、CGAffineTransformRotate? 對視圖做變焦旋轉(zhuǎn)
3、CGAffineTransformTranslate 對視圖在原來的位置上做平移
5、動畫
開始動畫,將改變屬性的代碼放在開始和提交之前
[UIView beginAnimations:nil context:nil]
提交動畫
[UIView commitAnimations]之間
當(dāng)動畫即將開始時執(zhí)行
+ (void)setAnimationWillStartSelector:(SEL)selector? 執(zhí)行delegate對象的selector
當(dāng)動畫結(jié)束時執(zhí)行
+(void)setAnimationDidStopSelector:(SEL)selector
動畫的持續(xù)時間,秒為單位
+ (void)setAnimationDuration:(NSTimeInterval)duration
動畫延遲delay秒后再開始
+ (void)setAnimationDelay:(NSTimeInterval)delay
動畫的開始時間,默認(rèn)為now
+ (void)setAnimationStartDate:(NSDate *)startDate
動畫的重復(fù)次數(shù)
+ (void)setAnimationRepeatCount:(float)repeatCount
如果設(shè)置為YES,代表動畫每次重復(fù)執(zhí)行的效果會跟上一次相反
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses
(3)
一、UIViewController的基本概念
1、見名知意,試圖控制器
3、每個試圖控制器都自帶一個試圖,并且負(fù)責(zé)這個試圖相關(guān)的一切事務(wù)
4、對于iOS應(yīng)用程序來說,每一個頁面都是都是一個控制器,通過切換不同的控制器,來實(shí)現(xiàn)切換不同的界面
4、在MVC體系中負(fù)責(zé)Model與View的通信
5、檢測設(shè)備旋轉(zhuǎn)以及內(nèi)存警告
想要App支持某個方向,需要兩個設(shè)置:
1) Deployment Info 中,勾選 Device Orientation
2) 在控制器中,重寫 supportedInterfaceOrientations 方法。(如不重寫,IPad 默認(rèn)是UIInterfaceorientationMaskAll , IPhone 默認(rèn)是 UIInterfaceOrientationMaskAllButUpsideDown )
模擬器模擬內(nèi)存警告
選中模擬器->hardware->simulate memory warning
6、UIViewController是所有試圖控制器類的基類,定義了試圖控制器的基本功能
二、UIViewController的生命周期
0、每次訪問控制器的view,view是nil的話就會自動調(diào)用該方法,如果重寫在這個方法內(nèi)必須給控制器設(shè)置一個根試圖,如果不給的話程序就會無限循環(huán)直到程序奔潰
loadView
1、控制器的View加載完畢
viewDidLoad
2、View即將顯示到window上
viewWillAppear
3、view顯示完畢
viewDidAppear
4、View即將從window上移除
viewWillDisappear
5、view從window上移除完畢
viewDidDisappear
6、view即將銷毀的時候調(diào)用
viewWillUnload
7、view銷毀完畢的時候調(diào)用
viewDidUnload
8、當(dāng)接收到內(nèi)存警告的時候
didReceiveMemoryWarning
9、具體流程查看課件中的圖片
五、模態(tài)試圖的使用
1、模態(tài)視圖不是專門的某個類,而是通過視圖控制器的presentModalViewController:方法彈出的視圖我們都稱為模態(tài)視圖
2、模態(tài)視圖出現(xiàn)的場景一般是臨時彈出的窗口,譬如:登陸窗口
3、通過設(shè)置將要彈出控制器的modalTransitionStyle屬性設(shè)置不同的動畫效果
3、彈出,
presentModalViewController:
4、取消:
dismissModalViewControllerAnimated: 方法關(guān)閉窗口
UIControl的事件
1、UIControlEventTouchDown
單點(diǎn)觸摸按下事件:用戶點(diǎn)觸屏幕,或者又有新手指落下的時候。
2、UIControlEventTouchDownRepeat
多點(diǎn)觸摸按下事件,點(diǎn)觸計數(shù)大于1:用戶按下第二、三、或第四根手指的時候。
3、UIControlEventTouchDragInside
當(dāng)一次觸摸在控件窗口內(nèi)拖動時。
4、當(dāng)一次觸摸在控件窗口之外拖動時
UIControlEventTouchDragOutside
5、當(dāng)一次觸摸從控件窗口之外拖動到內(nèi)部時
UIControlEventTouchDragEnter
6、當(dāng)一次觸摸從控件窗口內(nèi)部拖動到外部時
UIControlEventTouchDragExit
7、所有在控件之內(nèi)觸摸抬起事件
UIControlEventTouchUpInside
9、點(diǎn)擊手指抬起時
UIControlEventTouchUpInside
10、所有觸摸取消事件,即一次觸摸因?yàn)榉派狭颂嗍种付蝗∠蛘弑簧湘i或者電話呼叫打斷。
UIControlEventTouchCancel
(3)
一、UINavigationController(導(dǎo)航控制器)
1、基本概念
1)繼承于UIViewControler
2)相當(dāng)于一個容器用來管理有層級關(guān)系的控制器
3)采用棧的方式管理所有controller,每個controller管理各自的試圖。
棧:
向棧中添加一個對象的操作稱為入棧
在棧中刪除一個對象的操作稱為出棧
第一個入棧的對象叫做基棧
最后一個入棧的對象,叫做棧頂
當(dāng)前顯示的試圖控制器,即為棧頂。
對象出棧、入棧的方式:后進(jìn)先出,先進(jìn)后出
4)提供返回上一級controller的默認(rèn)button和方法
5)創(chuàng)建時要給導(dǎo)航控制器設(shè)置根控制器
2、結(jié)構(gòu),包含三個部分
1)navigationBar
2) 內(nèi)容試圖
3)toolBar,因?yàn)椴怀S茫J(rèn)是隱藏的
3、常用屬性和方法
屬性
1)獲取到在棧中最頂層的試圖控制器
topViewController
2)獲取到在棧中當(dāng)前顯示的試圖控制器
visibleViewController
3)在棧中當(dāng)前有的試圖控制器
viewControllers
4)隱藏導(dǎo)航欄
navigationBarHidden
5)獲取到導(dǎo)航欄
navigationBar
方法
1)初始化一個根視圖控制器,在棧的最底層
initWithRootViewController:(UIViewController *)rootViewController;
2)往棧中壓入一個新的控制器
pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
3)移除棧頂?shù)囊粋€控制器
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
4)彈出到指定的視圖控制器中,返回的數(shù)組代表要出棧中移除的對象
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
5)回到根視圖控制器
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
二、練習(xí)
在第二個視圖控制器在添加一個UIButton實(shí)例,單擊該按鈕時,壓入一個新的視圖控制器。在該視圖中添加四個按鈕,依次實(shí)現(xiàn),
1、壓入一個新的視圖控制器;
2、返回上一個視圖控制器;
3、返回根視圖控制器;
4、返回指定的視圖控制器
三、UINavigationBar(導(dǎo)航欄)
1、基本概念
1)和導(dǎo)航控制器一樣,是一個容器,用來顯示導(dǎo)航欄上的試圖,
2) 豎屏44 橫屏32
2、配置外觀
1) 設(shè)置導(dǎo)航欄樣式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
2)設(shè)置導(dǎo)航欄是否透明,設(shè)為不透明能夠屏蔽導(dǎo)航欄對試圖frame的影響
self.navigationController.navigationBar.translucent = NO;
1) 配置背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
2)配置背景圖片
self.navigationController.navigationBar setBackgroundImage: forBarMetrics:
3) 改變導(dǎo)航欄上默認(rèn)按鈕顏色
self.navigationBar.tintColor
3、配置內(nèi)容
1)每個controller都有專屬的navigationItem
1)通過配置不同控制器的navigationItem讓不同控制器的導(dǎo)航欄顯示不同的內(nèi)容
2)navigationItem的常用屬性
leftBarButtonItem
rightBarButtonItem
backBarButtonItem
title
titleView
3) backBarButtonItem是由上一層控制器控制的
4) 導(dǎo)航欄的prompt屬性,通過navigationItem來設(shè)置,其主要作用是用于提示用戶。比如,用戶正在請求網(wǎng)絡(luò)數(shù)據(jù)時,提示用戶數(shù)據(jù)正在加載。待加載完成后可以將它的值設(shè)置為nil,取消顯示。
四、練習(xí)
將自己項(xiàng)目的注冊按鈕放到右上角,修改系統(tǒng)自帶的返回按鈕
(4)
一、分欄控制器的基本概念
1、UITabBarController和UINavigationController一樣是用來管理試圖控制器的
2、與導(dǎo)航控制器不同,tab控制器使用數(shù)組管理子試圖控制器的,并且子試圖之間是平等關(guān)系,導(dǎo)航控制器所管理的試圖控制器之間上
練習(xí):
1、創(chuàng)建若干個子視圖控制器(它們是并列的關(guān)系)
2、創(chuàng)建一個數(shù)組,將已創(chuàng)建的子視圖控制器,添加到數(shù)組中
3、創(chuàng)建UITabBarController實(shí)例
4、tabBarController.viewControllers = viewControllers;
5、添加到window的rootViewController中
二、分欄試圖控制器的結(jié)構(gòu)
1、有兩部分組成
1)contentView:顯示當(dāng)前controller的view
2) tabBar(標(biāo)簽欄):負(fù)責(zé)切換顯示controller, 高度為49
2、tabBar
1) 標(biāo)簽欄是唯一的,就好比導(dǎo)航控制器的導(dǎo)航欄
2) 設(shè)置分欄的顏色
barTintColor
3) 設(shè)置分欄的背景圖片
backgroundImage
4)標(biāo)簽欄的顯示與隱藏
hidden
5)設(shè)置導(dǎo)航控制器默認(rèn)顯示的控制器
selectedIndex
3、tabBarItem
1) 用來控制一組控制器的切換,類似選項(xiàng)卡,每個Tab控制一個試圖控制器,點(diǎn)擊哪個tab就顯示對應(yīng)的試圖控制器,當(dāng)前的試圖控制器
2) 每個tabBarItem都可以設(shè)置title、image/selectedImages、badgeValue
3) 設(shè)置選中的顏色
分欄控制器.tabBar.tintColor
3) TabBar只能顯示五個tab Item,如果超過五個則會自動生成個Morede 標(biāo)簽顯示剩余的Tab,這些Tab可以通過編輯顯示在UITabBar上
4) 自定義Item
[UITabBarItem alloc]initWithTitle: image: tag:
[UITabBarItem alloc]initWithTabBarSystemItem:tag:
3、支持國際化
1)找到plist文件
2)右鍵? add row
3)Localizations? 默認(rèn)就一個ENGLISH
4)在 添加一個 Item 1? Chinese (simplified)
練習(xí):
1、初始化我們需要在tabBarController中顯示的視圖控制器
2、初始化UItabBarItem
3、在子視圖控制器中添加UItabBarItem
4、我們將子視圖控制器放入數(shù)組中
5、初始化tabBarcontroller
6、將數(shù)組放入tabBar控制器中,方法viewControllers
三、代理監(jiān)聽分欄控制器的切換
1、視圖將要切換時調(diào)用,viewController為將要顯示的控制器,如果返回的值為NO,則無法點(diǎn)擊其它分欄了
- (BOOL)tabBarController:(UITabBarController *)tabBarControllershouldSelectViewController:(UIViewController *)viewController
2、視圖已經(jīng)切換后調(diào)用,viewController 是已經(jīng)顯示的控制器
- (void)tabBarController:(UITabBarController *)tabBarControllerdidSelectViewController:(UIViewController *)viewController
3、將要開始自定義item的順序
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
4、將要結(jié)束自定義item的順序
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
5、結(jié)束自定義item的順序
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
四、集成分欄控制器和導(dǎo)航控制器
1、在Tab Bar控制器中某一個Tab中使用Navigation控制器,這是最常見的用法,我們將在下面具體講解這個用法
2、在一個Navigation控制器控制下的某一個或某些控制器是Tab Bar控制器,這時對該Tab Bar控制器的壓入和彈出方法和普通視圖控制器一樣
五、總結(jié)
1、UINavigationController、UITabBarController和UIViewController通常都是組合出現(xiàn)的,一定要熟練使用
2、UINavigationController、UITabBarController都是UIViewController的子類,管理的都是UIViewContrller
3、UINavigationController可以嵌套UITabBarController
4、UITabBarController可以嵌套UINavigationController
(5)
一、表視圖的介紹
1、表視圖,是iOS中最重要的試圖,很多應(yīng)用程序都會使用到,
2、表試圖里面可以放很多行信息
3、表視圖的兩種風(fēng)格
1)普通風(fēng)格
UITableViewStylePlain
2)分組風(fēng)格
UITableViewStyleGrouped
3)UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實(shí)并沒有本質(zhì)區(qū)別,只是后者按分組樣式顯示前者按照普通樣式顯示而已。
二、表視圖的基本結(jié)構(gòu)
1、表視圖有表頭、表尾、中間一連串單元格試圖組成
1)設(shè)置表頭
tableHeaderView
2)設(shè)置單元格試圖
UITableViewCell,單元格也可以分段顯示,每一段都可以通過代理設(shè)置段頭和段尾
2)設(shè)置表尾
tableFooterView
3) tableView的常用屬性和方法
設(shè)置表視圖分割線風(fēng)格
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;
設(shè)置表視圖分割線顏色,默認(rèn)標(biāo)準(zhǔn)灰色
@property(nonatomic,retain) UIColor *separatorColor;
設(shè)置表視圖的頭部視圖
@property(nonatomic,retain) UIView *tableHeaderView;
設(shè)置表視圖的尾部視圖
@property(nonatomic,retain) UIView *tableFooterView;
設(shè)置表視圖單元格的行高
@property(nonatomic) CGFloat rowHeight;
設(shè)置表視圖背景
@property(nonatomic, readwrite, retain) UIView *backgroundView
刷新表視圖單元格中數(shù)據(jù)
- (void)reloadData;
顯示指示條
showsVerticalScrollIndicator
設(shè)置表視圖section頭部行高
@property(nonatomic) CGFloat sectionHeaderHeight;
設(shè)置表視圖section尾部部行高
@property(nonatomic) CGFloat sectionFooterHeight
三、單元格的顯示
1、單元格的位置表示
NSIndexPath:能表示當(dāng)前cell是tableView的第幾段第幾行
2、單元格的創(chuàng)建
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell的樣式
1)UITableViewCellStyleDefault
左側(cè)顯示textLabel,imageView顯示在最左邊
2)UITableViewCellStyleValue1
左側(cè)顯示textLabel、右側(cè)顯示detailTextLabel,imageView顯示在最左邊
3)UITableViewCellStyleValue2
左側(cè)依次顯示textLabel(默認(rèn)藍(lán)色)和detailTextLabel,imageView可選
4)UITableViewCellStyleSubtitle
左上方顯示textLabel,左下方顯示detailTextLabel,imageView顯示在最左邊
cell的輔助圖標(biāo) accessoryType
1) 不顯示任何圖標(biāo)
UITableViewCellAccessoryNone,
2) 跳轉(zhuǎn)指示圖標(biāo)
UITableViewCellAccessoryDisclosureIndicator
3) 內(nèi)容詳情圖標(biāo)和跳轉(zhuǎn)指示圖標(biāo)
UITableViewCellAccessoryDetailDisclosureButton
4) 勾選圖標(biāo)
UITableViewCellAccessoryCheckmark
5) 內(nèi)容詳情圖標(biāo)
UITableViewCellAccessoryDetailButton
5) 自定義輔助圖標(biāo)
accessoryView屬性
3、cell的常用屬性
1)設(shè)置cell的背景試圖
backgroundView
2)設(shè)置選中的cellbei的背景圖片
selectedBackgroundView
3) 設(shè)置選中時的樣式
selectionStyle
練習(xí):不分組的名人錄
四、數(shù)據(jù)源方法(UITableViewDatasource)
1、實(shí)例化表視圖時,必須要實(shí)現(xiàn)他的數(shù)據(jù)源方法,以此來完成表中數(shù)據(jù)的配置,一般來說數(shù)據(jù)源方法是用來配置表中的數(shù)據(jù)
2、常用數(shù)據(jù)源方法
1)配置section中含有行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
2)創(chuàng)建單元格實(shí)例
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
3) 配置表視圖section個數(shù),默認(rèn)為1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
4)section中的頭部視圖的標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
5)section中的尾部視圖的標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
/* 表視圖的編輯 移動、刪除等 */
6)指定單元格是否支持編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
7)指定單元格是否支持移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
8)用戶編輯了哪一個單元格,在這里執(zhí)行刪除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath;
9)實(shí)現(xiàn)此方法,移動單元格
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
五、代理方法(UITableViewDelegate)
1、一般是處理表視圖基本樣式(單元格高度)以及捕捉選中單元格事件
2、常用代理方法
1)配置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
2)設(shè)置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)用戶單擊單元格中輔助按鈕時,調(diào)用該方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
5)用戶單擊單元格,調(diào)用該方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
6)取消選中單元格時,調(diào)用該方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
7)設(shè)置單元格編輯樣式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
練習(xí):分組的名字錄
1、設(shè)置段頭、段尾
2、自定義段頭段尾
一、表視圖常用屬性和方法
屬性
1、設(shè)置表視圖分割線風(fēng)格
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;
2、設(shè)置表視圖分割線顏色,默認(rèn)標(biāo)準(zhǔn)灰色
@property(nonatomic,retain) UIColor *separatorColor;
3、設(shè)置表視圖的頭部視圖
@property(nonatomic,retain) UIView *tableHeaderView;
4、設(shè)置表視圖的尾部視圖
@property(nonatomic,retain) UIView *tableFooterView;
5、設(shè)置表視圖單元格的行高
@property(nonatomic) CGFloat rowHeight;
6、設(shè)置表視圖背景
@property(nonatomic, readwrite, retain) UIView *backgroundView
7、刷新表視圖單元格中數(shù)據(jù)
- (void)reloadData;
8、設(shè)置表視圖section頭部行高
@property(nonatomic) CGFloat sectionHeaderHeight;
9、設(shè)置表視圖section尾部部行高
@property(nonatomic) CGFloat sectionFooterHeight;
10、 刷新表視圖section中數(shù)據(jù)
- (void)reloadSectionIndexTitles
11、默認(rèn)為NO,不可以編輯,設(shè)置時,不存在動畫效果
@property(nonatomic,getter=isEditing) BOOL editing;
12、覆蓋此方法,存在動畫效果
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
13、默認(rèn)為YES,當(dāng)表視圖不在編輯時,單元格是否可以選中
@property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);
14、默認(rèn)為NO,當(dāng)表視圖在編輯時,單元格是否可以選中
@property(nonatomic) BOOL allowsSelectionDuringEditing;
15、默認(rèn)為NO,是否可以同時選中多個單元格,注意版本問題
@property(nonatomic) BOOL allowsMultipleSelection
17、 默認(rèn)為NO,在編輯狀態(tài)下時,是否可以同時選中多個單元格,注意版本問題
@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing
方法
1、指定一個cell,返回一個NSIndexPath實(shí)例,如果cell沒有顯示,返回nil
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;
2、指定一個范圍,返回一個數(shù)組,內(nèi)容是NSIndexPath實(shí)例,指定rect無效,返回nil
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;
3、指定一個NSIndexPath,返回一個cell實(shí)例,如果cell沒有顯示,返回為nil
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
4、根據(jù)顯示的cell,返回一組cell實(shí)例的數(shù)組,如果沒有顯示,返回nil
- (NSArray *)visibleCells;
5、根據(jù)顯示的cell,返回一組NSIndexPath實(shí)例的數(shù)組,如果沒有顯示,返回nil
- (NSArray *)indexPathsForVisibleRows;
6、滑動到指定的位置,可以配置動畫
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
7、插入一行cell,指定一個實(shí)現(xiàn)動畫效果
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
8、刪除一行cell, 指定一個實(shí)現(xiàn)動畫效果
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
9、刷新一個行cell,指定一個實(shí)現(xiàn)動畫效果
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
10、移動cell的位置,指定一個實(shí)現(xiàn)動畫效果
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath NS_AVAILABLE_IOS(5_0);
(6)
-、表視圖的編輯狀態(tài)
1、表視圖的編輯狀態(tài)有兩種
insert和delete
2、實(shí)現(xiàn)表視圖編輯的步驟
1)讓tableview處于編輯狀態(tài)
self.tableView.editing
2)通過代理方法確定tableView處于哪種狀態(tài)(添加還是刪除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
3)選擇添加或者刪除通過代理方法來做不同的處理
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
二、單元格的移動
只需實(shí)現(xiàn)兩個代理方法
1、實(shí)現(xiàn)代理方法,讓tableView的單元格支持移動,如果該方法返回為NO,則不支持單元格的移動,該方法一般可省略
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
2、實(shí)現(xiàn)代理方法,指定從哪里移到哪里
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
一、謂詞的基本概念
cocoa中提供了NSPredicate類,指定過濾器的條件,將符合條件的對象保留下來
二、創(chuàng)建謂詞的步驟
1、設(shè)置謂詞條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"];
2、使用謂詞
1) 判斷當(dāng)前對象是滿足條件
[predicate evaluateWithObject:id]
2) 將數(shù)組中滿足條件的內(nèi)容翻到數(shù)組中并返回該數(shù)組
[array filteredArrayUsingPredicate:predicate];
三、運(yùn)算符,謂詞中的字符串用單引號括起來
1、邏輯運(yùn)算符 &&(AND) ||(OR)
[NSPredicate predicateWithFormat:@"age<25 || age>27"];
2、根據(jù)關(guān)鍵字查詢 IN
[NSPredicate predicateWithFormat:@"name in {'tom-8','jack-3','xxx'}"];
[NSPredicate predicateWithFormat:@"name in %@",inArray];
3、檢查某個字是否以**開頭? BEGINSWITH
[NSPredicate predicateWithFormat:@"name BEGINSWITH 't' "];
4、檢查某個單詞是否已**結(jié)尾 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)運(yùn)動事件
3)遠(yuǎn)程控制事件
2、UIView不接收觸摸事件的三種情況
1.不接收用戶交互
userInteractionEnabled = NO
2.隱藏
hidden = YES
3.透明
alpha = 0.0 ~ 0.01
提示:UIImageView的userInteractionEnabled默認(rèn)就是NO,因此UIImageView以及它的子控件默認(rèn)是不能接收觸摸事件的
二、事件處理基本方法
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、觸摸序列被諸如電話呼入這樣的系統(tǒng)事件取消
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
三、UITouch觸摸對象
當(dāng)用戶觸摸屏幕時,事件會被封裝成一個event實(shí)例,包含了用戶觸摸的相關(guān)信息,event實(shí)例中包含著若干個UITouch實(shí)例,一個touch代表著用戶的一個手指
1、UITouch常用屬性
1)window
觸摸產(chǎn)生時所處的窗口
2)view
觸摸產(chǎn)生時所處的試圖
3)tapCount
tap(輕擊)操作,和鼠標(biāo)單獨(dú)單擊操作類似,tapCount表示短時間內(nèi)輕擊屏幕的次數(shù),因此可以根據(jù)tapCount判斷單擊、雙擊或更多的輕擊
雙擊試圖是時單擊也會執(zhí)行的解決方法
if (touch.tapCount == 1) {
//延遲0.5秒執(zhí)行 runLoop會計算這個時間
[self performSelector:@selector(singleTap) withObject:nil afterDelay:0.5];
}else{
//告訴runLoop取消調(diào)用某個延遲的方法
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
[self doubleTap];
}
4)timestamp
記錄了觸摸事件產(chǎn)生或變化時的時間,單位是秒
5)phase
觸摸事件在屏幕上有一個周期,即觸摸開始、觸摸點(diǎn)移動、觸摸點(diǎn)結(jié)束,還有中途取消,通過phase可以查看當(dāng)前觸摸事件在一個周期中所處的狀態(tài),pase是一個枚舉,包含:
//觸摸開始
UITouchPhaseBegan
//接觸點(diǎn)移動
UITouchPhaseMoved
//接觸點(diǎn)無移動
UITouchPhaseStationary
//觸摸結(jié)束
UITouchPhaseEnded
//觸摸取消
UITouchPhaseCancelled
2、UITouch常用方法
1)返回一個CGPoint類型的值,表示觸摸在view這個視圖上的位置,這里返回的位置是針對view的坐標(biāo)系的
- (CGPoint)locationInView:(UIView *)view
2)該方法記錄了前一個坐標(biāo)值
- (CGPoint)previousLocationInView:(UIView *)view:
一、事件傳遞
1、從事件發(fā)生到其處理的對象,傳遞要經(jīng)過特殊的一段過程,當(dāng)用戶點(diǎn)擊設(shè)備屏幕時,iOS捕捉到一系列的觸摸,將其打包到UIEvent對象并放置到應(yīng)用程序活動事件隊列中
2、UIApplication對象從事件隊列中取出最前面的事件并將其分發(fā),通常,其將事件發(fā)送給應(yīng)用程序的主窗口-UIWindow實(shí)例,再由窗口對象發(fā)送事件給第一響應(yīng)者處理,一般通過touchesBegan方法獲取該事件
3、具體過程
1)先將事件對象由上往下傳遞(由父控件傳遞給子控件),找到最合適的控件來處理這個事件
2)調(diào)用最合適控件的touches方法
3)如果調(diào)用了[super touches...]方法,就會將事件順著響應(yīng)者鏈條往上傳遞,傳遞給上一個響應(yīng)者
4)接著就會調(diào)用上一個響應(yīng)者的touches...方法
二、響應(yīng)者鏈
1、基本概念
響應(yīng)者對象是一個能接受并處理事件的對象,UIResponser是所有響應(yīng)者對象的基類,該基類定義了一系列編程接口,不但為事件處理進(jìn)行服務(wù)而且還提供了通用的響應(yīng)行為處理,UIApplication、UIView(UIWindow)、UIViewController都直接或間接的繼承自UIResponser,所有的這些類的實(shí)例都是響應(yīng)者對象
響應(yīng)者鏈表示一系列的響應(yīng)者對象,事件被交由第一響應(yīng)者對象處理,如果第一響應(yīng)者不處理,事件被沿著響應(yīng)者鏈向上傳遞,交給下一個響應(yīng)者(nextresponder)
2、事件響應(yīng)者鏈傳遞的過程
1、當(dāng)用戶與試圖交互時,會將消息傳遞給試圖控制器,如果不存在控制器,傳遞給父試圖
2、如果不處理該消息,則繼續(xù)將消息向上傳遞,如果最上層的試圖也不處理,將事件交給window對象,最后交由UIApplication實(shí)例,如果不處理,丟棄事件
PS:傳遞方式
[self.nextResponder touchesBegan:touches withEvent:event];
3、通過響應(yīng)者鏈傳遞可以讓多個試圖響應(yīng)同一個手勢
三、練習(xí)
事件的傳遞演示
四、手勢識別器
UIGestureRecognizer類,用于檢測、識別用戶使用設(shè)備時所用的手勢,他是一個抽象類,定義了所有手勢的基本行為,以下是UIGestureRecognizer子類,用與處理具體的用戶手勢行為
1、輕擊
UITapGestureRecognizer
常用屬性
//點(diǎn)擊次數(shù)
numberOfTapsRequired
//消除兩個手勢的影響
requireGestureRecognizerToFail
//手指的數(shù)量,需將試圖的multipleTouchEnabled設(shè)為YES
numberOfTouchesRequired
2、輕掃
UISwipeGestureRecognizer
常用屬性
清掃方向,默認(rèn)是右
direction
3、長按
UILongPressGestureRecognizer
常用屬性
最小長按時間
minimumPressDuration
注意事項(xiàng)
如果不判斷會調(diào)用2次 按下2秒后自己調(diào)用一次 松開后又要調(diào)用一次
if (longPress.state == UIGestureRecognizerStateEnded) {
return;
}
NSLog(@"長按");
4、平移
UIPanGestureRecognizer
常用方法
獲取觸摸的位置
locationInView
注意事項(xiàng)
- (void)panAction:(UIPanGestureRecognizer *)gesture{
// 1.在view上面挪動的距離 //translationInView表示相對于起點(diǎn)挪動的位置是最新2點(diǎn)之間的間距
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
注意事項(xiàng):
每次調(diào)用過方法后,記得將scale置1,否則你的圖片在捏合一點(diǎn)便會變很大或很小
6、旋轉(zhuǎn)
UIRotationGestureRecognizer
注意事項(xiàng):
每次調(diào)用過方法后,記得將rotation置0,否則圖片旋轉(zhuǎn)不正常
7、手勢識別器的代理方法
/**
*? 是否允許多個手勢識別器同時有效
*? Simultaneously : 同時地
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
8、重要的事情說三遍
實(shí)現(xiàn)旋轉(zhuǎn)、縮放、平移的時候一定要清零
實(shí)現(xiàn)旋轉(zhuǎn)、縮放、平移的時候一定要清零
實(shí)現(xiàn)旋轉(zhuǎn)、縮放、平移的時候一定要清零
(8)
一、滑動試圖(UIScrollView)
1、基本概念
1) 移動設(shè)備的屏幕大小是極其有限的,因此直接展示在用戶眼前的內(nèi)容也相當(dāng)有限,當(dāng)展示的內(nèi)容較多,超出一個屏幕時,用戶可通過滾動手勢來查看屏幕以外的內(nèi)容
2) 通過scrollView可以通過手勢,放大或者縮小顯示的內(nèi)容
2、UIScrollView常用屬性
1)里面內(nèi)容的大小,也就是可以滾動的大小,模式是0,沒有滾動效果
contentSize
2)這個屬性能夠在UIScrollView的4周增加額外的滾動區(qū)域
contentInset
3) 這個屬性用來表示UIScrollView滾動的位置
contentOffset
4) 默認(rèn)是 yes,就是滾動超過邊界會反彈有反彈回來的效果。假如是 NO,那么滾動到達(dá)邊界會立刻停止
bounces
5) 是否分頁
pagingEnabled
6) 是否可以滾動
scrollEnabled
7) 滾動時是否顯示水平滾動條
showsHorizontalScrollIndicator
8) 滾動時是否顯示垂直滾動條
showsVerticalScrollIndicator
9) 滾動條的樣式,基本只是設(shè)置顏色。總共3個顏色:默認(rèn)、黑、白
indicatorStyle
10) 默認(rèn)是 NO,可以在垂直和水平方向同時運(yùn)動。當(dāng)值是 YES 時,假如一開始是垂直或者是水平運(yùn)動,那么接下來會鎖定另外一個方向的滾動。 假如一開始是對角方向滾動,則不會禁止某個方向
directionalLockEnabled
10) 讓指定的區(qū)域顯示顯示出來
scrollRectToVisible:animate
10)當(dāng)touch后還沒有拖動的時候值是YES,否則是NO
tracking
11) 當(dāng)內(nèi)容放大到最大或者最小的時候是YES,否則NO
zoomBouncing
12) 當(dāng)正在縮放的時候值是YES,否則為NO
zooming
13) 當(dāng)滾動后,手指放開但是還在繼續(xù)滾動中。這個時候是 YES,其它時候是 NO
decelerating
14) 設(shè)置手指放開后的減速率
decelerationRate
15) 一個浮點(diǎn)數(shù),表示能放最大的倍數(shù)
maximumZoomScale
16) 一個浮點(diǎn)數(shù),表示能縮最小的倍數(shù)
minimumZoomScale
17) 和 bounces 類似,區(qū)別在于:這個效果反映在縮放上面,假如縮放超過最大縮放,那么會反彈效果;假如是 NO,則到達(dá)最大或者最小的時候立即停止
bouncesZoom
3、UIScrollView代理方法
創(chuàng)建一個UIScrollView,并在里面放置一張圖片
1)scrollView已經(jīng)滑動
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
2)視圖已經(jīng)放大或縮小
- (void)scrollViewDidZoom:(UIScrollView *)scrollView;
3)scrollView開始拖動
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
4)scrollView結(jié)束拖動
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
5)scrollView開始減速(以下兩個方法注意與以上兩個方法加以區(qū)別)
- (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)返回一個放大或者縮小的視圖,要設(shè)置最大最小值
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
8)開始放大或者縮小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
9)縮放結(jié)束時
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;
10)是否支持滑動至頂部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
11)滑動到頂部時調(diào)用該方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
一、利用scrollView實(shí)現(xiàn)圖片的放大縮小
1、設(shè)置最大最小縮放倍率
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];
}
}
二、實(shí)現(xiàn)類似相冊的功能
1、創(chuàng)建一個scrollView,進(jìn)行配置
2、在scrollView里面放多張能夠顯示得圖片
3、每次翻到下一張圖片時要讓這上個圖片恢復(fù)到原比例
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、這些表格表面上類似于標(biāo)準(zhǔn)的UITableView控件,但是他們使用的dataSource和delegate方法有細(xì)微的差別二、UIPickerView常用方法? ? ? 1、獲取指定列的行數(shù)? ? - (NSInteger)numberOfRowsInComponent:(NSInteger)component;? 2、刷新所有列? ? - (void)reloadAllComponents;? 3、刷新指定的列? ? - (void)reloadComponent:(NSInteger)component;? 4、選擇一行? ? - (void)selectRow:(NSInteger)row inComponent:(NSInteger component? ? animated:(BOOL)animated;? 5、獲取指定列當(dāng)前顯示的是第幾行? ? - (NSInteger)selectedRowInComponent:(NSInteger)component;三、UIPickerView常用方法? ? 1、dataSource方法? ? ? 1)返回列數(shù)? ? ? ? - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView? ? ? 2)返回每一列對應(yīng)的行數(shù)? ? ? ? - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component? ? 2、delegate方法? ? ? ? 1)返回顯示的文本? ? ? ? - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component? ? ? ? 2)當(dāng)某一列選中的行數(shù)發(fā)生變化時調(diào)用? ? ? ? ? - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component? ? ? ? 3) 設(shè)置行高? ? ? ? - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component? ? ? ? 4)設(shè)置列寬? ? ? ? - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component? ? ? ? 5)自定義cell? ? ? ? - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view四、練習(xí)? ? 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];? ? ? 運(yùn)行,什么都沒有,因?yàn)槎嗌俣味嗌傩惺峭ㄟ^代理方法來設(shè)置的? ? ? 2)讓viewController類遵守兩個協(xié)議,在viewDidLoad方法里給pickerView掛上代理,并在viewController里實(shí)現(xiàn)如下兩個代理方法
//返回列數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
//返回每一列中的行數(shù)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 5;
}
運(yùn)行 選擇器里面有兩列,每列的數(shù)據(jù)都是用『?』表示,因?yàn)檫€沒有給每列賦值
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的內(nèi)容替換成如下內(nèi)容
if (component == 0) {
return data.count;
}
NSInteger selectedRow =? [pickerView selectedRowInComponent:component];
NSArray *tempArray = data[selectedRow][@"cities"];
return tempArray.count;
運(yùn)行 目前還是沒有數(shù)據(jù),但列數(shù)和行數(shù)已經(jīng)做了自動設(shè)置
5、給每行設(shè)置標(biāo)題,將如下代理方法寫入工程
//返回每個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;
}
運(yùn)行 每行有標(biāo)題 但左邊的省份變化,右邊的城市列表沒有跟著變化
6、實(shí)現(xiàn)右側(cè)跟著左側(cè)聯(lián)動
//當(dāng)某一列選中的行數(shù)發(fā)生變化時調(diào)用
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
//刷新指定列中的行
[pickerView reloadComponent:1];
//選擇指定的item
[pickerView selectRow:0 inComponent:1 animated:YES];
}
}
運(yùn)行 右側(cè)可以跟著左側(cè)聯(lián)動
7、設(shè)置列寬和行高,在工程里添加如下代理方法
- (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;
}
運(yùn)行 只剩色塊,而里面卻沒有內(nèi)容了,因?yàn)檫@個代理方法執(zhí)行- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component代理方法將不會再執(zhí)行
一、UIDatePicker時間拾取器
1、UIDatePicker提供了時間、日期供用戶選擇
2、UIDataPicker是對UIPickerView做了進(jìn)一步封裝,其外觀布局和UIPickerView完全一樣
二、UIDatePicker的常用屬性
1、初始化顯示的date日期
date
2、設(shè)置最小日期
minimumDate
3、設(shè)置最大日期
maximumDate
4、設(shè)置日期的顯示樣式
datePickerMode
UIDatePickerModeTime 顯示時間
UIDatePickerModeDate 顯示日期
UIDatePickerModeDateAndTime 顯示日期和時間
UIDatePickerModeCountDownTimer 顯示時間
5、分鐘間隔值
minuteInterval
三、練習(xí)
1、日期選擇器
1) 新建工程,在ViewController中的viewDidLoad加入如下代碼
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 736-300, 414, 300)];
datePicker.tag = 100;
//最小時間? 10年之前 不設(shè)置最小時間將沒有限定
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];
運(yùn)行 體驗(yàn)一下設(shè)置最小時間跟沒設(shè)置最小時間的區(qū)別
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)實(shí)現(xiàn)點(diǎn)擊事件方法
- (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);
}
運(yùn)行,查看效果
一、日期類簡單介紹
對日期我們經(jīng)常使用到的兩個類
1、NSDate,NSDate的對象表示一個具體的時間點(diǎn)
2、NSDateFormatter對象將時間轉(zhuǎn)化為字符串或者反轉(zhuǎn)
二、創(chuàng)建NSDate對象的幾種方式以及區(qū)別
1、獲取到GTM時間(世界標(biāo)準(zhǔn)時間),比中國時間早八個小時
NSDate *data = [NSDate date];
2、從當(dāng)前GTM時間往后推八個小時的時間,如果為負(fù)數(shù)就是往前推八個小時的時間
NSTimeInterval timeInterval = 8*60*60;
NSDate *chinaDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
3、從計算機(jī)時間(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、永遠(yuǎn)不可能到達(dá)的一個點(diǎn)
NSDate *futureDate = [NSDate distantFuture];
7、一個無限過去的時間點(diǎn)
NSDate *pastDate = [NSDate distantPast];
三、NSDate對象常用的方法
1、從計算機(jī)時間(1970-01-01 00:00:00)到date時間的時間差(秒為單位)
timeIntervalSince1970
2、從(2001-01-01 00:00:00)到date時間的時間差(秒為單位)
timeIntervalSinceReferenceDate
3、從當(dāng)前時間到date時間的時間差
timeIntervalSinceNow
4、當(dāng)前時間偏移多少秒后的新時間
dateByAddingTimeInterval
5、兩個日期之間的時間差
timeIntervalSinceDate
6、日期的比較
earlierDate//誰早返回誰
laterDate//誰晚返回誰
isEqualToDate//兩個日期是否相等
四、日期與字符串的轉(zhuǎn)換
1、日期格式如下:
y? 年
F? 月份中的周數(shù)
E? 周幾,EEEE星期幾
M 表示 月
m 表示 分
H 表示 24小時制
h 表示 12小時制
s 表示 秒
S 表示 毫秒
d? 月份中的天數(shù)
a? Am/pm
k? 一天中的小時數(shù)(1-24)
K? am/pm 中的小時數(shù)(0-11)
H? 一天中的小時數(shù)
h? am/pm 中的小時數(shù)(1-12)
2、字符串與日期的轉(zhuǎn)換
1)將日期轉(zhuǎn)換為字符串,轉(zhuǎn)換過后就自動換成系統(tǒng)所在時區(qū)的時間
[dateFormatter stringFromDate:date]
2)將字符串轉(zhuǎn)化為日期
[dateFormatter dateFromString:str]
--------------------------------------------------------------------
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 未完(接UI控件及方法大集合續(xù))