gpt4 book ai didi

iphone - iOS背景图UIVIew和CALayer的关系

转载 作者:技术小花猫 更新时间:2023-10-29 10:37:21 26 4
gpt4 key购买 nike

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

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

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

  3. 如果我要添加标签/ View /按钮/等等。对于 View ,图层的背景图像是否会阻挡它们,因为 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 中的背景图片:

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

view.layer 中的背景图片:

background image in layer

最佳答案

  1. 默认情况下创建的 UIView 是不透明的。当您将 backgroundColor 设置为具有透明度的图案时,它会选择黑色作为背景色。您可以设置 customViewController.view.opaque = NO; 以允许显示您身后 View 的背景。

  2. 当您将图层的 backgroundColor 设置为具有透明度的图案时,您将绕过 UIView 逻辑,因此 View 的不透明性将被忽略; UIView 的转换也是如此。 CoreGraphics 和 friend 使用坐标系,其中正 y 轴指向上方。 UIKit 翻转这个坐标系。这就是图像上下颠倒的原因。

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

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

本质上,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 方法。我只是为了回应你的好奇心而发布了代码。

关于iphone - iOS背景图UIVIew和CALayer的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5916581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com