无法为cashape图层设置图像

时间:2017-08-27 03:53:28

标签: ios xcode

1.i已经创建了CAshapelayer并且还在CAshapelayer中成功插入了一个标签,但现在我正在尝试在图层中设置图像,但它无法正常工作。

  1. 帮助我为此图层制作放大动画。

         CAShapeLayer *circleLayer = [CAShapeLayer layer];
     [circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 250, 100, 100)] CGPath]];
    circleLayer.contents = (id)[[UIImage imageNamed:@"001-forward.png"] CGImage];
    circleLayer.contentsGravity=kCAGravityResize;
      //  circleLayer.masksToBounds = YES;
    [circleLayer setStrokeColor:[[UIColor brownColor] CGColor]];
    [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
    
     [[self.view layer] addSublayer:circleLayer];
    CATextLayer *label = [[CATextLayer alloc] init];
    [label setFont:@"Helvetica-Bold"];
    [label setFontSize:15];
    [label setFrame:CGRectMake(50,300, 100, 50)];
    [label setString:@"GM"];
    [label setAlignmentMode:kCAAlignmentCenter];
    [label setForegroundColor:[[UIColor greenColor] CGColor]];
    
    [circleLayer addSublayer:label];
    

2 个答案:

答案 0 :(得分:1)

这里的主要问题是您无法添加UIImage作为CAShape Layer的内容,正如您在UIImage as CAShapeLayer contents所见,所以我修改了您的代码,将Image添加到{ {1}}并将该图层添加到circleLayer

CALayer

enter image description here

答案 1 :(得分:0)

试试这个

CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 250, 100, 100)] CGPath]];
circleLayer.bounds = [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 250, 100, 100)] bounds];
circleLayer.masksToBounds = YES;
[circleLayer setStrokeColor:[[UIColor brownColor] CGColor]];
//Use .png if you store your image outside of .xcassets
circleLayer.fillColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"001-forward.png"]].CGColor;

[[self.view layer] addSublayer:circleLayer];