JMD
JMD

Reputation: 1590

Drawing a shape in drawRect removes gradient

I am very new to core graphics.

Basically I have a View that draws a gradient background, and I am attempting to create a subclass of this view with another, and then draw a Triangle shape in this subclass.

What happens is that the gradient isn't being drawn. I suspect because my subclass is drawing a triangle in drawRect (where the super draws its gradient) my subclass is overriding the drawRect method and the super's is being ignored.

How can I get both my gradient and my triangle to draw?

here is the code for my subclass's drawRect

- (void)drawRect:(CGRect)rect
{
     // get the current content
CGContextRef context = UIGraphicsGetCurrentContext();

CGFloat buttonHeight = self.bounds.size.height;
CGFloat buttonWidth = self.bounds.size.width;

// draw triangle
UIBezierPath *path = [UIBezierPath bezierPath];

[path moveToPoint:CGPointMake(buttonWidth / 2, buttonHeight * .75)];
[path addLineToPoint:CGPointMake(buttonWidth * .75, buttonHeight * .25)];
[path addLineToPoint:CGPointMake(buttonWidth * .25,buttonHeight * .25)];

CGContextAddPath(context, path.CGPath);
CGContextClip(context);
[_arrowColor set];
[path fill];
}

Upvotes: 0

Views: 173

Answers (1)

iain
iain

Reputation: 5683

If you call [super drawRect:rect]; at the start of the subclass' drawRect: then it'll draw the gradient first, then the triangle.

Upvotes: 1

Related Questions