2011-05-06 28 views
12

試圖理解UIView和CALayer之間的關係。我閱讀蘋果文檔,但沒有詳細描述兩者之間的關係。UIVIew與CALayer之間關於iOS背景圖像之間的關係

  1. 爲什麼當我添加背景圖片來查看「customViewController.view」時,我得到了不需要的圖像黑色。

  2. 當我將背景圖像添加到圖層「customViewController.view.layer」時,圖像的黑色區域消失了(這是我想要的),但背景圖像翻轉顛倒。這是爲什麼?

  3. 如果我要添加labels/views/buttons/etc。到視圖中,圖層的背景圖像是否會阻止它們,因爲CAlayer是由UIView支持的?

  4. 當你設置UIView的背景顏色時,它會自動設置相關圖層的背景顏色嗎?鑑於

    - (void)viewDidLoad 
    { 
        [super viewDidLoad]; 
        customViewController = [[CustomViewController alloc] init]; 
        customViewController.view.frame = CGRectMake(213, 300, 355, 315); 
    
        customViewController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login_background.png"]]; 
        // customViewController.view.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login_background.png"]].CGColor; 
    
        [self.view addSubview:customViewController.view]; 
    } 
    

背景圖像:在view.layer

background in view

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    customViewController = [[CustomViewController alloc] init]; 
    customViewController.view.frame = CGRectMake(213, 300, 355, 315); 

// customViewController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login_background.png"]]; 
    customViewController.view.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login_background.png"]].CGColor; 

    [self.view addSubview:customViewController.view]; 
} 

背景圖像:

background image in layer

回答

9
  1. 默認情況下UIView創建爲不透明。當您將backgroundColor設置爲具有透明度的圖案時,它將選擇黑色作爲背景顏色。您可以設置customViewController.view.opaque = NO;以允許您背後的視圖背景顯示。

  2. 當將層的backgroundColor設置爲具有透明度您繞過UIView的邏輯,所以視圖的不透明度被忽略的圖案; UIView的轉換也是如此。 CoreGraphics和朋友使用一個座標系統,正Y軸向上。 UIKit翻轉這個座標系。這就是圖像顛倒的原因。

  3. 如果您添加標籤/視圖/按鈕/等。該圖層將正確顯示在圖層背景圖案的頂部。

  4. 當您設置視圖的背景顏色時,它看起來好像圖層的背景顏色確實設置了。 (我沒有看到這個文件在任何地方)。

本質上的UIKit的UIView的東西是一個高層次接口這最終呈現到層。

希望這會有所幫助。

編輯5/7/2011

你可以得到圖像通過翻轉層的座標系統,但你不應該這樣做,以view.layer以顯示正確的方式了; UIKit並不期望你搞砸這個圖層,所以如果你翻轉它的座標系,任何UIKit繪圖都會翻轉;你需要使用一個子圖層。

所以,你的代碼應該是這樣的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    customViewController = [[CustomViewController alloc] init]; 
    customViewController.view.frame = CGRectMake(213, 300, 355, 315); 

    CALayer* l = [CALayer layer]; 
    l.frame = customViewController.bounds; 
    CGAffineTransform t = CGAffineTransformMake(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f); 
    l.affineTransform = t; 
    l.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage 
          imageNamed:@"login_background.png"]].CGColor; 
    [customViewController.view.layer addSublayer:l]; 

    [self.view addSubview:customViewController.view]; 
} 

注:通常當你翻轉座標,你有高度。對於圖層,您不需要執行此操作。我還沒有挖掘爲什麼這是如此。

正如你所看到的,這裏涉及到更多的代碼,這樣做並沒有真正的優勢。我真的建議你堅持使用UIKit的方法。爲了迴應你的好奇心,我只發佈了代碼。

+0

idz,非常感謝你的明確解釋。我想我會爲UIView設置背景圖片並且現在改變不透明設置。 – s2000coder 2011-05-07 00:19:01

+0

不客氣! – idz 2011-05-07 00:29:12

+0

好奇,如果你使用圖層而不是視圖,你如何修復y軸? – s2000coder 2011-05-07 00:40:23