Objective-c – Using CAGradientLayer as backround while also drawing in context with drawRect:

iphoneobjective c

I'm writing an app that plots some data into a simple graph and sometimes want to draw out the scale in the background. To do this I have a UIView subclass which acts as the graph background and simply use the drawRect: method to draw the scale (data elements will get added as subviews to this view, since I want the user to be able to interact with them).

However, I also want a gradient background color and have used CAGradientLayer for this purpose (as suggested in this thread). But when I add this as a sublayer, the gradient background appears, but nothing I do in drawRect: shows!

I'm sure I'm missing something simple or have misunderstood how to use CAGradientLayer or something, so any help or suggestions is appreciated!

This is the relevant code in my UIView subclass:

- (id)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

         // Create a gradient background
        CAGradientLayer *gradientBackground = [CAGradientLayer layer];
        gradientBackground.frame = self.bounds;
        gradientBackground.colors = [NSArray arrayWithObjects:(id)[[UIColor grayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
        [self.layer insertSublayer:gradientBackground atIndex:0];
    }

    return self;
}

- (void)drawRect:(CGRect)rect {

    // Get the graphics context
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Clear the context
    CGContextClearRect(context, rect);

    // Call actual draw method 
    [self drawInContext:context];
}

-(void)drawInContext:(CGContextRef)context {

CGFloat step;

// Draw Y scale
if (displayYScale) {

    CGContextSetRGBStrokeColor(context, 0, 0, 0, kScaleLineAlpha);

    if (yAxisScale.mode == FCGraphScaleModeData) {

        step = (self.frame.size.height-(yAxisScale.padding*2))/yAxisScale.dataRange.maximum;
        for (int i = 0; i <= yAxisScale.dataRange.maximum; i++) {

            CGContextMoveToPoint(context, 0.0f, (step*i)+yAxisScale.padding);
            CGContextAddLineToPoint(context, self.frame.size.width, (step*i)+yAxisScale.padding);
        }

    } else if (yAxisScale.mode == FCGraphScaleModeDates) {

        int units = (int)[yAxisScale units];
        step = (self.frame.size.height-(yAxisScale.padding*2))/units;
        for (int i = 0; i <= units; i++) {

            CGContextMoveToPoint(context, 0.0f, (step*i)+yAxisScale.padding);
            CGContextAddLineToPoint(context, self.frame.size.width, (step*i)+yAxisScale.padding);
        }
    }

    CGContextStrokePath(context);
}

// Draw X scale
if (displayXScale) {

    CGContextSetRGBStrokeColor(context, 0, 0, 255, kScaleLineAlpha);

    if (xAxisScale.mode == FCGraphScaleModeData) {

        step = (self.frame.size.width-(xAxisScale.padding*2))/xAxisScale.dataRange.maximum;
        for (int i = 0; i <= xAxisScale.dataRange.maximum; i++) {

            CGContextMoveToPoint(context, (step*i)+xAxisScale.padding, 0.0f);
            CGContextAddLineToPoint(context, (step*i)+xAxisScale.padding, self.frame.size.height);
        }

    } else if (xAxisScale.mode == FCGraphScaleModeDates) {

        int units = (int)[xAxisScale units];
        step = (self.frame.size.width-(xAxisScale.padding*2))/units;
        for (int i = 0; i <= units; i++) {

            CGContextMoveToPoint(context, (step*i)+xAxisScale.padding, 0.0f);
            CGContextAddLineToPoint(context, (step*i)+xAxisScale.padding, self.frame.size.height);
        }
    }

    CGContextStrokePath(context);
}
}

Thanks!

/Anders

Best Answer

Since you're drawing anyway, you can also just draw your own gradient:

// the colors
CGColorRef topColor = [[UIColor grayColor] CGColor];
CGColorRef bottomColor = [[UIColor whiteColor] CGColor];
NSArray *colors =
    [NSArray arrayWithObjects: (id)topColor, (id)bottomColor, nil];
CGFloat locations[] = {0, 1};

CGGradientRef gradient =
    CGGradientCreateWithColors(CGColorGetColorSpace(topColor),
                               (CFArrayRef)colors, locations);

// the start/end points
CGRect bounds = self.bounds;
CGPoint top = CGPointMake(CGRectGetMidX(bounds), bounds.origin.y);
CGPoint bottom = CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds));

// draw
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLinearGradient(context, gradient, top, bottom, 0);

CGGradientRelease(gradient);
Related Topic