iPhone編程添加遞進(jìn)式子視圖實(shí)例學(xué)習(xí)
iPhone編程添加遞進(jìn)式子視圖實(shí)例學(xué)習(xí)是本文要介紹的內(nèi)容,主要是來系統(tǒng)的來學(xué)習(xí)iphone編程中的一些細(xì)節(jié)和視圖的相關(guān)問題,來看本文內(nèi)容詳解。iPhone編程中心采用兩種重要的范型:
面向?qū)ο蠓缎?/strong>
模型-視圖-控制器(MVC)設(shè)計(jì)模式
iPhone上的MVC劃分:
視圖
由UIView類以及子類以及其相關(guān)的UIViewController類提供。PS:關(guān)于此處Controller,個(gè)人認(rèn)知為一個(gè)容器,用于保存UIView類的實(shí)例,同時(shí)負(fù)責(zé)對屏幕中各項(xiàng)進(jìn)行布局。
控制器
通過3中關(guān)鍵技術(shù)實(shí)現(xiàn):委托、目標(biāo)操作和通知
模型
模型方法拖過數(shù)據(jù)源和數(shù)據(jù)含義等協(xié)議提供數(shù)據(jù),需要實(shí)現(xiàn)由控制器觸發(fā)的回調(diào)方法。
實(shí)例學(xué)習(xí)
在實(shí)踐Pdf版書中P47的例子中,通過蘋果官網(wǎng)資料學(xué)習(xí):
UIView類
常用屬性:
- superview
- subviews
- window
- autoresizingMask UIViewAutoresizing常量,
- 常用UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin
- autoresizesSubviews 標(biāo)記子視圖是否自動(dòng)重設(shè)大小
- tag 標(biāo)識(shí)視圖的標(biāo)簽
常用方法:
- addSubview
- insertSubview
- removeFromSuperview
- viewWithTag 通過標(biāo)識(shí)獲取視圖指針
UIViewController類
為蘋果應(yīng)用程序提供基本的view-management(視圖管理)模型。
UINavigationController,UITabBarController是此類的子類,提供額外行為操作來管理復(fù)雜層次關(guān)系視圖控制器和視圖。
常用屬性:
view:
當(dāng)前控制器管理的視圖,為控制器當(dāng)前可見的視圖
常用方法:
loadView:
創(chuàng)建視圖用于控制器使用。
willRotateToInterfaceOrientation:
用于屏幕翻轉(zhuǎn)告知控制器,以響應(yīng)對應(yīng)的改變
shouldAutorotateToInterfaceOrientation:
返回一個(gè)Bool值,來指示當(dāng)前視圖控制器是否支持指定的方向
此函數(shù)涉及一個(gè)常量,如下:
UIInterfaceOrientation常量:
- typedef enum {
- UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
- UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
- } UIInterfaceOrientation;
用于判斷用戶的屏幕橫放和豎放是否支持。比如:看視頻,一般都是橫起看,那么取值只能UIDeviceOrientationLandscapeRight UIDeviceOrientationLandscapeLeft。完整代碼如下:
- // Allow the view to respond to 2 iPhone Orientation changes,like:
- -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIDeviceOrientationLandscapeRight || interfaceOrientation == UIDeviceOrientationLandscapeLeft);
- }
幾何類型結(jié)構(gòu):
CGRect
- A structure that contains the location and dimensions of a rectangle.
- struct CGRect {
- CGPoint origin;
- CGSize size;
- };
- typedef struct CGRect CGRect;
CGPoint
- A structure that contains a point in a two-dimensional coordinate system.
- struct CGPoint {
- CGFloat x;
- CGFloat y;
- };
- typedef struct CGPoint CGPoint;
CGSize
- A structure that contains width and height values.
- struct CGSize {
- CGFloat width;
- CGFloat height;
- };
- typedef struct CGSize CGSize;
CGFloat
- The basic type for all floating-point values.
- typedef float CGFloat;// 32-bit
- typedef double CGFloat;// 64-bit
小結(jié):iPhone編程添加遞進(jìn)式子視圖實(shí)例學(xué)習(xí)的內(nèi)容介紹完了,希望本文能對你有所幫助!