title: CAShapeLayer filling with another CAShapeLayer as a mask
body: <p>My question is about filling animation one <code>CAShapeLayer</code> with another <code>CAShapeLayer</code>. So, I wrote some code that almost achieves my goal.</p>
  
  <p>I created a based layer and fill it with another. Code below:</p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      _shapeLayer = [CAShapeLayer layer];
      [self drawBackgroundLayer];
  }
  // my main/backgound layer
  - (void)drawBackgroundLayer {
      _shapeLayer.frame = CGRectMake(0, 0, 250, 250);
      _shapeLayer.lineWidth = 3;
      _shapeLayer.strokeColor = [UIColor blackColor].CGColor;
      _shapeLayer.fillColor = UIColor.whiteColor.CGColor;
      _shapeLayer.path = [UIBezierPath bezierPathWithRect:_shapeLayer.bounds].CGPath;
  
      [self.view.layer addSublayer:_shapeLayer];
  
      [self createCircleLayer];
  
  }
  </code></pre>
  
  <p>Create a circled mask layer for filling:</p>
  
  <pre><code>- (void)createCircleLayer {
  
      _shapeLayer.fillColor = UIColor.greenColor.CGColor;
  
      CAShapeLayer *layer = [CAShapeLayer layer];
  
      CGFloat radius = _shapeLayer.bounds.size.width*sqrt(2)/2;
  
      layer.bounds = CGRectMake(0, 0, 2*radius, 2*radius);
  
      layer.position = CGPointMake(_shapeLayer.bounds.size.width/2, _shapeLayer.bounds.size.height/2);
  
      UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:layer.bounds cornerRadius:radius];
      layer.path = path.CGPath;
  
      layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
      _shapeLayer.mask = layer;
  
  }
  </code></pre>
  
  <p>// our result is next]</p>
  
  <p><a href="https://i.stack.imgur.com/mjLHU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mjLHU.jpg" alt="enter image description here"></a></p>
  
  <p>So, let's animate it. I just transform the layer to fill the whole "_shapeLayer".</p>
  
  <pre><code>- (void)animateMaskLayer:(CAShapeLayer *)maskLayer {
  
      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
  
      animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1.0)];
      animation.duration = 1;
      animation.removedOnCompletion = NO;
      animation.fillMode = kCAFillModeForwards;
      [newLayer addAnimation:animation forKey:@"transform"];
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/13FUk.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/13FUk.gif" alt="enter image description here"></a></p>
  
  <p>Now, I want to do the same with this green layer (fill it with red color). 
  But when I use the mask I have red circle and the white background what is obviously.
  My goal here is to have red circle and green background. Like normal filling with another color.
  By using inverse mask won't help neither. 
  So, I just have no ideas how to do it. </p>
  
  <p><a href="https://i.stack.imgur.com/E5vAD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E5vAD.png" alt="enter image description here"></a> </p>
  
  <p>If you suggest using:</p>
  
  <pre><code>[_shapeLayer addSublayer:layer];
  </code></pre>
  
  <p>it won't work as there will be not only square but different paths and clipToBounds is not an option.</p>
  
  <p>Thank you for any help!</p>