Iphone iOS上关于背景图像的UIVIew和CALayer之间的关系
试图理解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的背景
- (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中的背景图像:
customViewController.view.opaque=NO
允许您后面视图的背景显示出来- (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上方的某个地方绘制图像。