【问题标题】:Relationship between UIVIew and CALayer regarding background image on iOSiOS上UIVIew和CALayer关于背景图片的关系
【发布时间】:2011-08-20 11:16:11
【问题描述】:

试图理解UIView和CALayer的关系。我阅读了 Apple 文档,但没有详细描述两者之间的关系。

  1. 为什么当我添加背景图片来查看“customViewController.view”时,我得到了不需要的黑色图像。

  2. 当我将背景图像添加到图层“customViewController.view.layer”时,图像的黑色区域消失了(这是我想要的),但是背景图像被颠倒了。这是为什么呢?

  3. 如果我要添加标签/视图/按钮/等。到view,会不会因为CAlayer是UIView支持的,所以layer的背景图会挡住它们?

  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];
    }
    

视图中的背景图片:

- (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 中的背景图片:

【问题讨论】:

    标签: iphone ios uiview uikit calayer


    【解决方案1】:
    1. 默认创建为不透明的 UIView。当您将 backgroundColor 设置为具有透明度的图案时,它选择黑色作为背景颜色。您可以设置customViewController.view.opaque = NO; 以允许您背后的视图背景显示出来。

    2. 当您将图层的背景颜色设置为具有透明度的图案时,您将绕过 UIView 逻辑,因此忽略视图的不透明性; UIView 的变换也是如此。 CoreGraphics 和朋友使用正 y 轴向上的坐标系。 UIKit 翻转了这个坐标系。这就是图像出现倒置的原因。

    3. 如果您添加标签/视图/按钮/等。将正确显示在图层背景图案的顶部。

    4. 当您设置视图的背景颜色时,它看起来好像确实设置了图层的背景颜色。 (我在任何地方都没有看到这方面的记录)。

    本质上,UIKit 的 UIView 是一个高级界面,最终呈现在图层上。

    希望这会有所帮助。

    2011 年 5 月 7 日编辑

    您可以通过翻转图层的坐标系来使图像以正确的方式显示,但您不应该对 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 方法。我只是为了响应您的好奇心而发布了代码。

    【讨论】:

    • idz,非常感谢您的清晰解释。我想我现在会为 UIView 设置背景图像并更改不透明设置。
    • 很好奇,如果你使用图层而不是视图,你如何修复 y 轴?
    • 嘿,Blu,我刚刚更新了答案,向您展示了一种方法;有几个。如您所见,UIKit 方法更简单。
    • 谢谢 idz,我只是想知道用于学习目的。我不打算用图层来做。
    • 当我为我的图层设置 affineTransform 时,它会在整个 UIView 上方的某处绘制图像。
    猜你喜欢
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多