Iphone iOS上关于背景图像的UIVIew和CALayer之间的关系

Iphone iOS上关于背景图像的UIVIew和CALayer之间的关系,iphone,ios,uiview,uikit,calayer,Iphone,Ios,Uiview,Uikit,Calayer,试图理解UIView和CALayer之间的关系。我阅读了苹果的文档,但没有详细描述两者之间的关系 为什么当我将背景图像添加到“customViewController.view”视图时,会得到不需要的黑色图像 当我将背景图像添加到图层“customViewController.view.layer”时,图像的黑色区域消失了(这是我想要的),但背景图像被颠倒了。为什么呢 如果我将标签/视图/按钮等添加到视图中,层的背景图像是否会阻止它们,因为CAlayer由UIView支持 设置UIView的背景

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

  • 为什么当我将背景图像添加到“customViewController.view”视图时,会得到不需要的黑色图像

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

  • 如果我将标签/视图/按钮等添加到视图中,层的背景图像是否会阻止它们,因为CAlayer由UIView支持

  • 设置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中的背景图像:

  • 默认情况下,UIView创建为不透明。将背景色设置为具有透明度的图案时,它将选择黑色作为背景色。您可以设置
    customViewController.view.opaque=NO
    允许您后面视图的背景显示出来

  • 当您将图层的背景色设置为具有透明度的图案时,您绕过了UIView逻辑,因此忽略了视图的不透明性;UIView的转换也是如此。CoreGraphics和friends使用正y轴指向上的坐标系。UIKit翻转此坐标系。这就是为什么图像看起来是颠倒的

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

  • 设置视图的背景色时,看起来好像确实设置了图层的背景色。(我在任何地方都没有看到记录)

  • 从本质上讲,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方法。我只是出于你的好奇心才发布了代码

    idz,非常感谢你的清晰解释。我想我现在将为UIView设置背景图像,并更改不透明设置。好奇的是,如果要使用图层而不是视图,如何修复y轴?嘿,Blu,我刚刚更新了答案,向您展示了一种方法;有几个。正如你所看到的,UIKit方法更简单。谢谢你,idz,我只是想了解一下,以供学习之用。我不打算用图层来做这件事。当我将仿射变换设置到我的图层时,它会在整个UIView上方的某个地方绘制图像。