本文介绍了如何使用SVGKImage更改SVG填充颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用SVGKImage更改SVG填充颜色?

how to use SVGKImage change SVG fill color?

SVGKImage *svgImage = [SVGKImage imageNamed:@"card.svg"];
svgImage.size = self.bounds.size;

SVGKLayeredImageView *imageLayer = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
SVGKLayer *layer = (SVGKLayer *)imageLayer.layer;
self.layer.mask = layer;

在这里,我找到一种简单的方法来获取CAShapeLayer

Here, I find a easy way to get the CAShapeLayer

SVGKImage * svgImage = [SVGKImage imageNamed:@test.svg];
CAShapeLayer * thisLayer = svgImage.CALayerTree;
// thisLayer在test.svg中有UIBezierPath信息,因此,你可以用它做任何事情。

SVGKImage *svgImage = [SVGKImage imageNamed:@"test.svg"]; CAShapeLayer *thisLayer = svgImage.CALayerTree;//thisLayer has the UIBezierPath info in test.svg,so,you can do anything with it.

推荐答案

您必须使用 CALayer 子图层来更改颜色:

You have to use the CALayer sub-layers to changing the color :

SVGKImage *svgImage = [SVGKImage imageNamed:@"Anchor.svg"];
SVGKLayeredImageView *svgImageView = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
[capturedImageView addSubview:svgImageView];

CALayer* layer = svgImageView.layer;
for (CALayer *subLayer in layer.sublayers) {
    DLog(@"%@", [subLayer class]);

    for (CALayer *subSubLayer in subLayer.sublayers) {
        DLog(@"%@", [subSubLayer class]);

        for (CALayer *subSubSubLayer in subSubLayer.sublayers) {
            DLog(@"%@", [subSubSubLayer class]);

            if( [subSubSubLayer isKindOfClass:[CAShapeLayer class]]){
                CAShapeLayer* shapeLayer = (CAShapeLayer*)subSubSubLayer;
                shapeLayer.fillColor = [UIColor redColor].CGColor;
            }
        }
    }
}

希望这有帮助。

来源:

这篇关于如何使用SVGKImage更改SVG填充颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:35