Rounded rect with Gradient color?

I read one a while back about masking UIViews. I think the same applies pretty much on all objects which use drawRect.

Up vote 2 down vote favorite 3 share g+ share fb share tw.

I have not really done much programming with Core Graphics. And I tend to stick with QuartzCore now that it does a lot of what I need through the layer property :) However, I have a UIView, which is currently gradient. I'd like to add rounded corners to this UIView and the layer property does not do this when I draw the gradient: - (void)drawRect:(CGRect)rect { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGGradientRef glossGradient; CGColorSpaceRef rgbColorspace; size_t num_locations = 2; CGFloat locations2 = { 0.0, 1.0 }; CGFloat components8 = { 1.0, 1.0, 1.0, 0.95, // Start color 1.0, 1.0, 1.0, 0.60 }; // End color rgbColorspace = CGColorSpaceCreateDeviceRGB(); glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); CGRect currentBounds = self.

Bounds; CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); CGGradientRelease(glossGradient); CGColorSpaceRelease(rgbColorspace); } I am not really sure where I should be rounding in the drawRect method. Thanks. Iphone core-graphics link|improve this question edited Jan 16 '10 at 8:29Peter Hosey53.5k445137 asked Dec 14 '09 at 20:43runmad1,5691748 53% accept rate.

I've updated my answer below with a working sample. Please let me know if that works for you. – nash Dec 17 '09 at 18:14.

I read one a while back about masking UIViews. I think the same applies pretty much on all objects which use drawRect stackoverflow.com/questions/996292/how-t... Here's what I did, and it works fine as far as I can tell. First, I borrowed Mr NilObject's code snippet from the above mentioned post.

I modified it to fit in an object (as he wrote it as a C function instead of a method) I subclass UIView to create my own custom view. I overload initWithRect: to make my background transparent. So basically: set transparent background (in init), or clipping will be uggly in drawRect, first clip, then draw inside the clipped area The following is a working example: // // TeleView.

M // #import "TeleView. H" @implementation TeleView /**** in init methods, set background to transparent, otherwise, clipping shows a black background ****/ - (id) initWithFrame:(CGRect)frame { if((self = super initWithFrame:frame)) { self setBackgroundColor:UIColor colorWithWhite:0.0f alpha:0.0f; } return self; } - (void) clipCornersToOvalWidth:(float)ovalWidth height:(float)ovalHeight { float fw, fh; CGContextRef context = UIGraphicsGetCurrentContext(); CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size. Width, self.frame.size.

Height); if (ovalWidth == 0 || ovalHeight == 0) { CGContextAddRect(context, rect); return; } CGContextSaveGState(context); CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGContextScaleCTM (context, ovalWidth, ovalHeight); fw = CGRectGetWidth (rect) / ovalWidth; fh = CGRectGetHeight (rect) / ovalHeight; CGContextMoveToPoint(context, fw, fh/2); CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); CGContextClosePath(context); CGContextRestoreGState(context); } -(void)drawRect:(CGRect)rect { CGContextRef currentContext = UIGraphicsGetCurrentContext(); /**** here is what I modified. ****/ self clipCornersToOvalWidth:20.0f height:20.0f; CGContextClip(currentContext); /**** below this is your own code ****/ CGGradientRef glossGradient; CGColorSpaceRef rgbColorspace; size_t num_locations = 2; CGFloat locations2 = { 0.0, 1.0 }; CGFloat components8 = { 1.0, 0.0, 0.0, 0.60, // Start color 0.0, 1.0, 0.0, 0.40 }; // End color rgbColorspace = CGColorSpaceCreateDeviceRGB(); glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); CGRect currentBounds = self. Bounds; CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); CGGradientRelease(glossGradient); CGColorSpaceRelease(rgbColorspace); } @end.

Yeah, I have seen this one before. I just can't merge it with my own code :( – runmad Dec 15 '09 at 3:24 What happens when you try then? What do you see?

Any errors? How have you tried to integrate ir? – nash Dec 15 '09 at 6:06 It's either or.. If I round the corners I cannot use the gradient code, and vice versa.

I am sure I am merging the two incorrectly, but CG isn't really something I have much experience with. – runmad Dec 15 '09 at 23:29 I'll have a look at it later today, if I can. I'll post what I can come up with.

– nash Dec 15 '09 at 8:38 Thanks that did it! – runmad Dec 15 '09 at 23:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions