您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 第四章:UIView 的幾個基礎知識點

編輯:iOS 開發綜合

要談 iOS 的版面,得先搞懂三個 C 結構體與三個類別。這些東西看起來瑣碎,但之後每一次設定 frame、每一次 debug 版面都會用到。

一、三個幾何結構體

三者都定義在 CGGeometry.h 裡。

CGPoint:一個點

struct CGPoint {
    CGFloat x;
    CGFloat y;
};

CGSize:一個尺寸

struct CGSize {
    CGFloat width;
    CGFloat height;
};

CGRect:一個矩形

struct CGRect {
    CGPoint origin;   // 偏移量是相對父視圖的
    CGSize  size;
};

二、CGRectMake:把矩形做出來

CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height) {
    CGRect rect;
    rect.origin.x = x; rect.origin.y = y;
    rect.size.width = width; rect.size.height = height;
    return rect;
}

給定左上角座標與寬高,就得到一個位置與大小都確定的矩形。它被宣告為 inline 函式,一是因為體積小,二是因為畫面排版對效率有要求。

三、UIScreen:螢幕這塊板子

CGRect screenBounds = [[UIScreen mainScreen] bounds];            // 含狀態列的整個螢幕
CGRect viewBounds   = [[UIScreen mainScreen] applicationFrame];  // 不含狀態列的可用區域

以舊的 320×480 螢幕為例:

  • screenBounds:origin (0, 0),size (320, 480)。
  • applicationFrame:origin (0, 20),size (320, 460)——因為狀態列佔了 20pt。

兩者都是相對於「裝置螢幕」而言,這點要特別留意:view 的 frame 是相對父視圖,兩者的座標系統不同,混用就會出現莫名的偏移。

四、UIView 與 UIWindow

UIView 繼承自 UIResponder,它就是那塊「畫布」。如果把 window 當成畫框,我們做的事就是不斷在畫框上更換、疊加畫布,或在畫布上再疊畫布,大小由開發者決定:

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 400.0)];

UIWindow 繼承自 UIView。乍看有點奇怪——畫框怎麼會繼承畫布?但仔細想,畫框的形狀本來就等同畫布,只是被加強過(能成為 key window、能接收系統事件)。這也是為什麼一個 view 可以直接 addSubview 到另一個 view 上。

系統啟動時建立 window 的典型寫法:

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor grayColor];
[self.window makeKeyAndVisible];

五、動手練習:用 frame 拼版面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // 先印出來比較 bounds 與 applicationFrame 的差異
    CGRect bound = [[UIScreen mainScreen] bounds];
    NSLog(@"bound: %f, %f, %f, %f", bound.origin.x, bound.origin.y, bound.size.width, bound.size.height);
    CGRect appBound = [[UIScreen mainScreen] applicationFrame];
    NSLog(@"appBound: %f, %f, %f, %f", appBound.origin.x, appBound.origin.y, appBound.size.width, appBound.size.height);

    // 第一塊:藍色,320 × 100
    UIView *v_one = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 100)];
    v_one.backgroundColor = [UIColor blueColor];
    [self.window addSubview:v_one];

    // 第二塊:紅色,注意 y 從 100 開始
    UIView *v_two = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100, 160, 100)];
    v_two.backgroundColor = [UIColor redColor];
    [self.window addSubview:v_two];

    // 第三塊:綠色,接在右邊
    UIView *v_three = [[UIView alloc] initWithFrame:CGRectMake(160, 100, 160, 100)];
    v_three.backgroundColor = [UIColor greenColor];
    [self.window addSubview:v_three];

    // 第四塊:橘色
    UIView *v_four = [[UIView alloc] initWithFrame:CGRectMake(0.0, 260, 320, 200)];
    v_four.backgroundColor = [UIColor orangeColor];
    [self.window addSubview:v_four];

    // 第五塊:黃色,故意與前面重疊,用來觀察 z 軸順序
    UIView *v_five = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 160, 200)];
    v_five.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:v_five];

    self.window.backgroundColor = [UIColor grayColor];
    [self.window makeKeyAndVisible];

    [v_one release]; [v_two release]; [v_three release]; [v_four release]; [v_five release];
    return YES;
}

跑起來會看到五塊不同顏色的矩形。兩個值得動手驗證的實驗:

  1. 把第五塊的程式碼移到最前面,觀察重疊區域的顏色變化——後加入的 subview 永遠在上層。
  2. 把某個 view 的 y 設成負值或超出 window 範圍,看它被裁掉的情形。

這套「用 frame 絕對定位」的思維,跟網頁早期用 DIV 做定位很類似。後來 iOS 推出 Auto Layout 與 constraint,是為了處理多尺寸螢幕的相對關係;但 frame 仍是底層,理解它之後再看 Auto Layout 會清楚很多。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved