简体   繁体   English

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

[英]Relationship between UIVIew and CALayer regarding background image on iOS

Trying to understand the relationship between UIView and CALayer. 试图了解UIView和CALayer之间的关系。 I read Apple documentation but it doesn't describe in detail the relationship between the two. 我阅读了Apple文档,但没有详细描述两者之间的关系。

  1. Why is it that when I add the background image to view "customViewController.view", I get the unwanted black color of the image. 为什么当我添加背景图像来查看“customViewController.view”时,我得到图像的不需要的黑色。

  2. And when I add the background image to the layer "customViewController.view.layer", the black area of the image is gone (which is what I wanted), but the background image is flipped upside down. 当我将背景图像添加到图层“customViewController.view.layer”时,图像的黑色区域消失了(这就是我想要的),但背景图像是颠倒翻转的。 Why is that? 这是为什么?

  3. If I were to add labels/views/buttons/etc. 如果我要添加标签/视图/按钮/等。 to the view, will the layer's background image block them because CAlayer is backed by UIView? 对于视图,图层的背景图像是否会阻止它们,因为CAlayer由UIView支持?

  4. When you set the background color of a UIView does it automatically set the background color of the associated layer? 当您设置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]; } 

Background image 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 view.layer: 在view.layer中的背景图像:

图层中的背景图片

  1. UIView as created opaque by default. UIView默认情况下创建为opaque。 When you set the backgroundColor to a pattern with transparency it is choosing black as the background color. 将backgroundColor设置为具有透明度的图案时,选择黑色作为背景颜色。 You can set customViewController.view.opaque = NO; 你可以设置customViewController.view.opaque = NO; to allow the background of the view behind yours to show through. 允许你背后的视图背景显示出来。

  2. When you set the backgroundColor of the layer to a pattern with transparency you are bypassing the UIView logic, so the opaqueness of the view is ignored; 当您将图层的backgroundColor设置为具有透明度的图案时,您将绕过UIView逻辑,因此忽略视图的不透明度; so also is the the UIView's transform. UIView的改造也是如此。 CoreGraphics and friends use a coordinate system where the positive y-axis points upwards. CoreGraphics和朋友使用坐标系统,其中正y轴指向上方。 UIKit flips this coordinate system. UIKit翻转了这个坐标系。 This is why the image appears upside down. 这就是图像颠倒的原因。

  3. If you add labels/views/buttons/etc. 如果你添加标签/视图/按钮/等。 the will appear correctly on top of the layer's background pattern. 将在图层的背景图案顶部正确显示。

  4. When you set the view's background color it appears as if the layer's background color is indeed set. 设置视图的背景颜色时,看起来好像设置了图层的背景颜色。 (I have not seen this documented anywhere). (我没有在任何地方看到这个记录)。

Essentially the UIKit's UIView stuff is a high-level interface which ends up rendered onto layers. 基本上UIKit的UIView东西是一个高级界面,最终渲染到图层上。

Hope this helps. 希望这可以帮助。

EDIT 5/7/2011 编辑2011年5月7日

You can get the image to show the right way up by flipping the layer's coordinate system BUT you shouldn't do this to view.layer; 你可以通过翻转图层的坐标系来让图像显示正确的方向但是你不应该这样做来查看。 UIKit doesn't expect you to mess with this layer, so if your flip its coordinate system any UIKit drawing will be flipped; UIKit不希望你弄乱这个图层,所以如果翻转它的坐标系,任何UIKit图形都会被翻转; you need to use a sublayer. 你需要使用一个子层。

So your code would look like this: 所以你的代码看起来像这样:

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

Note: normally when you flip coordinates you include the height. 注意:通常在您翻转坐标时包含高度。 For layers you don't need to do this. 对于图层,您不需要这样做。 I haven't dug into why this is so. 我没有挖出为什么会这样。

As you can see there is a lot more code involved here and there is no real advantage to doing it this way. 正如您所看到的,这里涉及的代码更多,并且以这种方式实现它并没有真正的优势。 I really recommend you stick to the UIKit approach. 我真的建议你坚持使用UIKit方法。 I only posted the code in response to your curiosity. 我只是发布了代码以回应你的好奇心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM