关于iOS背景图像的UIVIew和CALayer之间的关系 [英] Relationship between UIVIew and CALayer regarding background image on iOS

查看:275
本文介绍了关于iOS背景图像的UIVIew和CALayer之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to understand the relationship between UIView and CALayer. I read Apple documentation but it doesn't describe in detail the relationship between the two.


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

  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.layer,图像的黑色区域消失了(这就是我想要的),但背景图像是颠倒翻转的。为什么?

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. Why is that?

如果我要添加标签/视图/按钮/等。在视图中,图层的背景图像是否会阻挡它们,因为CAlayer由UIView支持?

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?

当您设置UIView的背景颜色时,它会自动设置关联图层的背景颜色?

When you set the background color of a UIView does it automatically set the background color of the associated layer?

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

Background image in view.layer:

推荐答案


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

  1. UIView as created opaque by default. When you set the backgroundColor to a pattern with transparency it is choosing black as the background color. You can set customViewController.view.opaque = NO; to allow the background of the view behind yours to show through.

当您将图层的backgroundColor设置为具有透明度的图案时,您将绕过UIView逻辑,因此忽略视图的不透明度; UIView的改造也是如此。 CoreGraphics和朋友使用坐标系,正y轴指向上方。 UIKit翻转了这个坐标系。这就是图像颠倒的原因。

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; so also is the the UIView's transform. CoreGraphics and friends use a coordinate system where the positive y-axis points upwards. UIKit flips this coordinate system. This is why the image appears upside down.

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

If you add labels/views/buttons/etc. the will appear correctly on top of the layer's background pattern.

设置视图的背景颜色时,看起来好像图层的背景颜色确实已设置。 (我在任何地方都没有看到这个。)

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).

基本上UIKit的UIView内容是一个高级接口,结束向上渲染到图层上。

Essentially the UIKit's UIView stuff is a high-level interface which ends up rendered onto layers.

希望这会有所帮助。

编辑2011年5月7日

EDIT 5/7/2011

你可以通过翻转图层的坐标系来让图像显示正确的方向但是你不应该这样做来查看。 UIKit不希望你弄乱这个图层,所以如果翻转它的坐标系,任何UIKit图形都会被翻转;你需要使用一个子层。

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; 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.

正如您所看到的,这里涉及的代码要多得多,而且这样做并没有真正的优势。我真的建议你坚持使用UIKit方法。我只是发布了代码以回应你的好奇心。

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. I only posted the code in response to your curiosity.

这篇关于关于iOS背景图像的UIVIew和CALayer之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆