iPhone編程添加遞進式子視圖實例學習
iPhone編程添加遞進式子視圖實例學習是本文要介紹的內容,主要是來系統的來學習iphone編程中的一些細節和視圖的相關問題,來看本文內容詳解。iPhone編程中心采用兩種重要的范型:
面向對象范型
模型-視圖-控制器(MVC)設計模式
iPhone上的MVC劃分:
視圖
由UIView類以及子類以及其相關的UIViewController類提供。PS:關于此處Controller,個人認知為一個容器,用于保存UIView類的實例,同時負責對屏幕中各項進行布局。
控制器
通過3中關鍵技術實現:委托、目標操作和通知
模型
模型方法拖過數據源和數據含義等協議提供數據,需要實現由控制器觸發的回調方法。
實例學習
在實踐Pdf版書中P47的例子中,通過蘋果官網資料學習:
UIView類
常用屬性:
- superview
- subviews
- window
- autoresizingMask UIViewAutoresizing常量,
- 常用UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin
- autoresizesSubviews 標記子視圖是否自動重設大小
- tag 標識視圖的標簽
常用方法:
- addSubview
- insertSubview
- removeFromSuperview
- viewWithTag 通過標識獲取視圖指針
UIViewController類
為蘋果應用程序提供基本的view-management(視圖管理)模型。
UINavigationController,UITabBarController是此類的子類,提供額外行為操作來管理復雜層次關系視圖控制器和視圖。
常用屬性:
view:
當前控制器管理的視圖,為控制器當前可見的視圖
常用方法:
loadView:
創建視圖用于控制器使用。
willRotateToInterfaceOrientation:
用于屏幕翻轉告知控制器,以響應對應的改變
shouldAutorotateToInterfaceOrientation:
返回一個Bool值,來指示當前視圖控制器是否支持指定的方向
此函數涉及一個常量,如下:
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);
- }
幾何類型結構:
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
小結:iPhone編程添加遞進式子視圖實例學習的內容介紹完了,希望本文能對你有所幫助!