iOS 上 UIVIew 和 CALayer 关于背景图片的关系
试图理解UIView和CALayer之间的关系。我阅读了Apple文档,但它没有详细描述两者之间的关系。
为什么当我添加背景图像来查看“customViewController.view”时,我会得到不需要的黑色图像。
为什么当我将背景图像添加到“customViewController.view.layer”层时,图像的黑色区域消失了(这就是我想要的),但背景图像颠倒了。这是为什么?
如果我要添加标签/视图/按钮/等。到视图中,图层的背景图像是否会遮挡它们,因为 CAlayer 由 UIView 支持?
当您设置 UIView 的背景颜色时,它会自动设置关联图层的背景颜色吗?
- (void)viewDidLoad { [超级viewDidLoad]; customViewController = [[CustomViewController 分配] 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 中的背景图片:
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.
Why is it that when I add the background image to view "customViewController.view", I get the unwanted black color of the image.
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?
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?
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]; }
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIView 默认创建为不透明。当您将backgroundColor 设置为具有透明度的图案时,它会选择黑色作为背景色。您可以设置
customViewController.view.opaque = NO;
以允许您后面的视图的背景显示出来。当您将图层的背景颜色设置为具有透明度的图案时,您将绕过 UIView 逻辑,因此视图的不透明性将被忽略; UIView 的变换也是如此。 CoreGraphics 和朋友使用正 y 轴指向上方的坐标系。 UIKit 翻转这个坐标系。这就是图像看起来颠倒的原因。
如果您添加标签/视图/按钮/等。将正确显示在图层背景图案的顶部。
当您设置视图的背景颜色时,看起来就像确实设置了图层的背景颜色一样。 (我没有在任何地方看到过此记录)。
本质上,UIKit 的 UIView 东西是一个高级界面,最终渲染到层上。
希望这有帮助。
编辑 5/7/2011
您可以通过翻转图层的坐标系来使图像以正确的方式显示,但您不应该对 view.layer 执行此操作; UIKit 不希望你弄乱这个层,所以如果你翻转它的坐标系,任何 UIKit 绘图都会被翻转;你需要使用一个子层。
因此,您的代码将如下所示:
注意:通常,当您翻转坐标时,您会包含高度。对于图层,您不需要这样做。我还没有深究为什么会这样。
正如您所看到的,这里涉及更多的代码,并且这样做没有真正的优势。我强烈建议您坚持使用 UIKit 方法。我发布代码只是为了满足您的好奇心。
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.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).
Essentially the UIKit's UIView stuff is a high-level interface which ends up rendered onto layers.
Hope this helps.
EDIT 5/7/2011
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:
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. I only posted the code in response to your curiosity.