Ios 如何使CALayer边框alpha值与bgcolor不同

Ios 如何使CALayer边框alpha值与bgcolor不同,ios,objective-c,iphone,quartz-core,Ios,Objective C,Iphone,Quartz Core,我有一个UIView,它有一个alpha值为0.5、背景颜色为白色的图层。我需要有一个相同白色的边框,但是alpha值为1。不幸的是,边界的alpha值始终与bgcolor的相同。因此,我的问题是: 我可以使边框的alpha与背景颜色不同吗? 如果我不能,你有什么建议吗? 试试这个。这应该只是更改边框颜色和alpha而已 [self.yourView.layer setBorderColor:[UIColor颜色带红色:0.0绿色:0.0蓝色:0.0 alpha:1.0].CGColor];当然

我有一个UIView,它有一个alpha值为0.5、背景颜色为白色的图层。我需要有一个相同白色的边框,但是alpha值为1。不幸的是,边界的alpha值始终与bgcolor的相同。因此,我的问题是:

我可以使边框的alpha与背景颜色不同吗? 如果我不能,你有什么建议吗?
试试这个。这应该只是更改边框颜色和alpha而已


[self.yourView.layer setBorderColor:[UIColor颜色带红色:0.0绿色:0.0蓝色:0.0 alpha:1.0].CGColor];当然,您可以根据需要更改RGB值。

试试这个。这应该只是更改边框颜色和alpha而已

[self.yourView.layer setBorderColor:[UIColor颜色带红色:0.0绿色:0.0蓝色:0.0 alpha:1.0].CGColor];当然,您可以根据需要更改RGB值

我可以使边框的alpha与背景颜色不同吗

对。你可以这样做:

CALayer * firstLayer = [[CALayer alloc] init];

[firstLayer setFrame:CGRectMake(100, 100, 100, 100)];

firstLayer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
firstLayer.borderWidth = 3.0f;
firstLayer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5].CGColor;
另一种方法是在相同大小的另一层中设置一层。为外层提供所需的边界厚度,并确保其不透明度为100%。第二层应具有分数不透明度:

CALayer * firstLayer = [[CALayer alloc] init];
CALayer * secondLayer = [[CALayer alloc] init];

[firstLayer setFrame:CGRectMake(100, 100, 100, 100)];
[secondLayer setFrame:CGRectMake(0, 0, 100, 100)];
[firstLayer addSublayer:secondLayer];

firstLayer.borderColor = [UIColor redColor].CGColor;
firstLayer.borderWidth = 1.0f;

secondLayer.backgroundColor = [UIColor redColor].CGColor;
secondLayer.opacity = 0.8f;

[self.view.layer addSublayer:firstLayer];
我可以使边框的alpha与背景颜色不同吗

对。你可以这样做:

CALayer * firstLayer = [[CALayer alloc] init];

[firstLayer setFrame:CGRectMake(100, 100, 100, 100)];

firstLayer.borderColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0].CGColor;
firstLayer.borderWidth = 3.0f;
firstLayer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5].CGColor;
另一种方法是在相同大小的另一层中设置一层。为外层提供所需的边界厚度,并确保其不透明度为100%。第二层应具有分数不透明度:

CALayer * firstLayer = [[CALayer alloc] init];
CALayer * secondLayer = [[CALayer alloc] init];

[firstLayer setFrame:CGRectMake(100, 100, 100, 100)];
[secondLayer setFrame:CGRectMake(0, 0, 100, 100)];
[firstLayer addSublayer:secondLayer];

firstLayer.borderColor = [UIColor redColor].CGColor;
firstLayer.borderWidth = 1.0f;

secondLayer.backgroundColor = [UIColor redColor].CGColor;
secondLayer.opacity = 0.8f;

[self.view.layer addSublayer:firstLayer];
只需使用:

view.layer.borderColor = [[UIColor redColor] colorWithAlphaComponent:0.6].CGColor;
view.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];
只需使用:

view.layer.borderColor = [[UIColor redColor] colorWithAlphaComponent:0.6].CGColor;
view.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.2];

谢谢这是一个更完整的答案。谢谢。这是一个更完整的答案。