I'm using this sample code from Palimondo: it is brilliant code learning how to mask CAGradientLayer's onto CAShapeLayers. The Output is as follows:
Now I am trying to create this image:
Using PaintCode, I was able to get all the UIBezierPath's and Gradient Colors and stops and put them into multiple Arrays, and essentially iterate through each one adding it to new CAShapeLayer and CAGradientLayer as such:
- (void)viewDidLoad
{
[super viewDidLoad];
[self blackPaths];
[self setupGradients];
int count = 0;
for (CAGradientLayer *gradientLayerItem in _individualGradientArrays) {
CAShapeLayer *gradientMask = [CAShapeLayer layer];
gradientMask.fillColor = [[UIColor clearColor] CGColor];
gradientMask.strokeColor = [[UIColor blackColor] CGColor];
gradientMask.lineWidth = 4;
gradientMask.frame = CGRectMake(0, 0, _testView.bounds.size.width, _testView.bounds.size.height);
//
// CGMutablePathRef t = CGPathCreateMutable();
// CGPathMoveToPoint(t, NULL, 0, 0);
// CGPathAddLineToPoint(t, NULL, _testView.bounds.size.width, _testView.bounds.size.height);
gradientMask.path = [[_UIBezierPathsArray objectAtIndex:count]CGPath];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.locations = gradientLayerItem.locations;
gradientLayer.startPoint = CGPointMake(0.5,1.0);
gradientLayer.endPoint = CGPointMake(1.0,0.5);
gradientLayer.frame = CGRectMake(0, 0, _testView.bounds.size.width, _testView.bounds.size.height);
// NSMutableArray *colors = [NSMutableArray array];
// for (int i = 0; i < 3; i++) {
// [colors addObject:(id)[[UIColor colorWithHue:(0.2 * i) saturation:1 brightness:.8 alpha:1] CGColor]];
// }
gradientLayer.colors = gradientLayerItem.colors;
[gradientLayer setMask:gradientMask];
[_testView.layer addSublayer:gradientLayer];
count++;
}
[_testView setNeedsDisplay];
The goal is to have 16 individual CAGradientLayer's that I can animate using CoreAnimation to create a sort of "swirling" effect (I could just place the output from PaintCode into a drawRect, and it works, the problem is that I was told that I would have a much more difficult time animating either the colors or gradients via CoreGraphics rather than placing them in Layers).
And then, this is outputed:
After 100's of iterations and before this point, I tried swaping out the [UIColor clearColor]
for whiteColor
(Which is not what the sample code required) and this is outputted:
You can see there is only a couple of gradients, and it is my belief the whiteColor
is interfering with the CAGradientLayer.
I know I must be missing something extremely subtle, but I can't seem to see it.
Why is it that in the example code, we are able to use clearColor
and have the gradient show up appropriately, yet using clearColor
with my UIBezierPath's attached to my CAShapeLayer's the gradients break?
A friend mentioned that another solution would be to have 3 circles of the gradients and basically rotate them concentrically over the masked shape creating the illusion, however I am at a loss how I would be able to do that.