简体   繁体   English

CALAYER应用背景颜色和图案图像

[英]CALAYER apply background color plus pattern image

I am having fun with Core Animation stuffs. 我正在玩核心动画的东西。 I needed to apply a pattern image to a CALAyer. 我需要将模式图像应用于CALAyer。 That part work perfectly with something like that: 那部分完美地与这样的东西:

color = UIColor(patternImage: UIImage(named: "myPatternImage")! 
shapeLayer.fillColor = color.CGColor;

But now I need to apply a background color behind the pattern. 但现在我需要在模式后面应用背景颜色。 my Patter image is a png (with alpha component). 我的Patter图像是一个png(带有alpha分量)。 So I want to apply a simple Color to the shape and after apply the pattern Image. 所以我想在形状上应用一个简单的颜色,然后应用图案Image。 Is there a way to do that on the fly? 有没有办法在飞行中做到这一点?

Just add another sublayer with background color and add it before your pattern background layer: 只需添加另一个具有背景颜色的子图层并将其添加到图案背景图层之前:

UIColor *backgroundColor = [UIColor redColor];
CALayer *solidBackgroundColorLayer = [[CALayer alloc] init];
solidBackgroundColorLayer.backgroundColor = backgroundColor.CGColor;
[[self layer] addSublayer:solidBackgroundColorLayer];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"MyPattern.png"]];
backgroundLayer = [[CALayer alloc] init];
[backgroundLayer setBackgroundColor:[color CGColor]];
[[self layer] addSublayer:backgroundLayer];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM