Skip to main content
 首页 » 操作系统

ios之CALAYER 应用背景颜色加图案图像

2024年01月01日43Renyi-Fan

我对 Core Animation 的东西很感兴趣。 我需要将图案图像应用到 CALAyer。 该部分与类似的东西完美配合:

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

但现在我需要在图案后面应用背景色。我的 Patter 图像是 png(带有 alpha 组件)。 所以我想对形状应用简单的颜色,然后在应用图案图像之后。 有没有办法即时做到这一点?

请您参考如下方法:

只需添加另一个具有背景颜色的子层并将其添加到您的图案背景层之前:

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