Corners from CGContextAddArc are too dark - iphone

i have little problem with my code. I want to add a footer to a uitableview-section, but that's not my problem.
My problem is that i want to have rounded corners on my footer-view, but not on all corners, just the two on the bottom. But the corners are just too dark and i don't know why...
Heres my code:
- (void)drawRect:(CGRect)rect {
float radius = 10.0f;
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat strokeColorArray[4] = {0.35f,0.35f,0.35f,1.0f};
CGContextSetStrokeColor(context, strokeColorArray);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1);
CGContextSetLineWidth(context, 1.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius, radius, 0, M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI / 2, M_PI, 0);
CGContextClosePath(context);
CGContextFillPath(context);
CGContextMoveToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius, radius, 0, M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI / 2, M_PI, 0);
CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextDrawPath(context, kCGPathStroke);
}
Here's a picture of my problem:
http://img195.imageshack.us/img195/9834/bildkw.png
Can you see the darker corners? How can i fix that?
Thank you
Sebastian

There's a couple things you can try. One is to change the radius of the corners. Another is to add some code to make sure that you draw lines on the inside of the rectangle and instead of centering the lines on the edges. To do that, you would have to inset the rect by half of the line width, or 0.5 pixels in this case.

Related

Why is my CoreGraphics shape not showing a border?

This is the code I have:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [BUTTON_COLOR CGColor]);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetLineWidth(context, 1);
int radius = CORNER_RADIUS;
CGContextMoveToPoint(context, 0, self.frame.size.height / 2);
CGContextAddLineToPoint(context, POINT_WIDTH, self.frame.size.height);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height);
CGContextAddArc(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius,
radius, 0.0f, -M_PI / 2, 1);
CGContextAddLineToPoint(context, POINT_WIDTH, 0);
CGContextAddLineToPoint(context, 0, self.frame.size.height / 2);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
Why is it not drawing a border around the shape?
EDIT: Now it draws the shape, thanks to changing the last line, but it draws an extra line around it, like this:
http://cl.ly/0u0e051a060m161u0n08
Use CGContextDrawPath(context, kCGPathFillStroke) instead of CGContextFillPath() and CGContextStrokePath().
The thing is, after the first call to CGContextFillPath() or CGContextStrokePath() the context's path is cleared, so the second call does not have a path to work with.

iphone Drawing gradient inside a polygon

I want to draw a gradient inside a polygon using core graphics. I am able to draw a hexagon using the code below but i don't know how to fill this hexagon with a gradient color. Any help will be really appreciated.
- (void)drawRect:(CGRect)rect
{
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGPoint center;
center = CGPointMake(210.0, 90.0);
CGContextMoveToPoint(context, center.x, center.y + 60.0);
for(int i = 1; i < 6; ++i)
{
CGFloat x = 60.0 * sinf(i * 2.0 * M_PI / 6.0);
CGFloat y = 60.0 * cosf(i * 2.0 * M_PI / 6.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y);
}
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}
Use following code for Gradient fill
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
0.62890625, 0.859375, 10.9921875, 1.00,
0.65625, 0.8046875, 0.9453125, 1.00,
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(rgb);
// now we can actually create the shape and fill it
CGPoint start, end;
CGContextSetRGBFillColor(context, 0, 0, 0.6, 0.1);
CGContextFillEllipseInRect(context, CGRectMake(0.0, 0.0, 100.0, 100.0));
CGContextStrokeEllipseInRect(context, CGRectMake(0.0, 0.0, 100.0, 100.0));
// Gradient
CGRect myrect = CGRectMake(0.0, 0.0, rect.size.width, rect.size.height);
CGContextSaveGState(context);
CGContextClipToRect(context, myrect);
start = CGPointMake(myrect.origin.x, myrect.origin.y + myrect.size.height * 0.25);
end = CGPointMake(myrect.origin.x, myrect.origin.y + myrect.size.height * 1.0);
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation);
CGContextRestoreGState(context);

UIPopover in ipad

I would like to create something like this:
The copy, star, reply, details. Is this just a UIPopover with a segmented button on it?
Here's another example of what I want:
Anyone got something like this?
The class you are looking for is UIMenuController
This SO page has a lot of useful information.
No. Ive been trying to recreate that view with a UIPopoverViewcontroller, holding a UIViewController with a UISegment.. and Im not getting that same functionality. The main issue is the UIPopoverVC is giving me a 3d border around my viewcontroller/view.
It does appear that its a custom view. Which, on that note, would not be too difficult to create by the way, and that view can host your SegmentedViewController, just not in a UIPopoverVC.
And.. for your viewing enjoyment... here is the code that creates the CGPath for a "Balloon" view like the Popover creates. this code goes in your drawRect.
float radius=5; //corner radius
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextSetRGBFillColor(context, backColor.red,backColor.green,backColor.blue,backColor.alpha);
CGContextMoveToPoint(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect));
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect) + radius, radius, 3 * M_PI / 2, 0, 0);
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius, radius, 0, M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI / 2, M_PI, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) + radius, radius, M_PI, 3 * M_PI / 2, 0);
CGContextMoveToPoint(context, rect.size.width/2, rect.size.height);
CGContextAddLineToPoint(context, rect.size.width/2, rect.size.height+10);
CGContextAddLineToPoint(context, rect.size.width/2+10, rect.size.height);
CGContextClosePath(context);
CGContextFillPath(context);
CGContextRestoreGState(context);

Simple drawing with Quartz - Core Graphics

I want to draw a rectangle with the top-left corner rounded with 3 sub-spaces in it. Something like this:
_______
|_|_____|
| |
|_______|
But for some reason I cannot get the inner two lines drawn.
- (void) drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
float cornerRadius = 25.0;
float w = self.bounds.size.width;
float h = self.bounds.size.height;
CGContextMoveToPoint(context, cornerRadius, 0);
CGContextAddQuadCurveToPoint(context, 0, 0, 0, cornerRadius);
CGContextAddLineToPoint(context, 0, h);
CGContextAddLineToPoint(context, w, h);
CGContextAddLineToPoint(context, w, 0);
CGContextAddLineToPoint(context, cornerRadius, 0);
//drawing settings
CGContextSetLineWidth(context, 0.5);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor white].CGColor);
//draw rectangle
CGContextDrawPath(context, kCGPathFillStroke);
//draw title/label partition
CGContextMoveToPoint(context, TITLE_HEIGHT, 0);
CGContextAddLineToPoint(context, TITLE_HEIGHT, TITLE_HEIGHT);
CGContextStrokePath(context);
//draw title/content partition
CGContextMoveToPoint(context, 0, TITLE_HEIGHT);
CGContextAddLineToPoint(context, self.bounds.size.width, TITLE_HEIGHT);
CGContextStrokePath(context);
}
I wonder what am I mistaking here... ;(
Thanks in advance
Try to comment out the CGContextSetFillColorWithColor line for a while - maybe the rectangle is overlapping those lines?

How is a rounded rect view with transparency done on iphone?

A lot of apps pop up a transparent view with rounded corners and an activityIndicator when running a time consuming operation.
How is this rounding done and is it possible to do it just using Interface Builder (as there are lots of places I'd like to use something like this)? Or, should I use an imageview with a rounded rect or stretchable image? Do I need to draw the background myself?
So far, I have managed to get a basic view with similar transparency by setting the alphaValue in Interface Builder however it doesn't have rounded corners, and also the transparency seems to apply to all subviews (I don't want the text and activityindicator to be transparent, however even though I set the alphaValue on those in IB it seems to get ignored).
As of iPhone SDK 3.0, you can simply use the layer's cornerRadius property. E.g.:
view.layer.cornerRadius = 10.0;
Along the same lines, you can change the view's border color and width:
view.layer.borderColor = [[UIColor grayColor] CGColor];
view.layer.borderWidth = 1;
view.layer.cornerRadius = radius;
The hard way (that used to be required in the first iPhone SDK) is to create your own UIView subclass with drawRect: method:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0,0,0,0.75);
CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius,
radius, M_PI, M_PI / 2, 1); //STS fixed
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height);
CGContextAddArc(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius,
radius, 0.0f, -M_PI / 2, 1);
CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius,
-M_PI / 2, M_PI, 1);
CGContextFillPath(context);
Note: rect in this code should be taken from [self bounds] (or whatever location you want it in), it won't make sense with rect passed to drawRect: method.
I abstracted out #lostInTransit's response into this function:
static void ContextAddRoundedRect(CGContextRef c, CGRect rect, CGFloat radius) {
CGFloat minX = CGRectGetMinX(rect);
CGFloat maxX = CGRectGetMaxX(rect);
CGFloat minY = CGRectGetMinY(rect);
CGFloat maxY = CGRectGetMaxY(rect);
CGContextMoveToPoint(c, minX + radius, minY);
CGContextAddArcToPoint(c, maxX, minY, maxX, minY + radius, radius);
CGContextAddArcToPoint(c, maxX, maxY, maxX - radius, maxY, radius);
CGContextAddArcToPoint(c, minX, maxY, minX, maxY - radius, radius);
CGContextAddArcToPoint(c, minX, minY, minX + radius, minY, radius);
}
which places the path onto the context for you to do with as you may
slightly different CoreGraphics calls and i didn't close the path, in case you want to add that
CGContextFillPath(c);
In your view do this in the drawRect method
float radius = 5.0f;
CGRect rect = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
rect = CGRectInset(rect, 1.0f, 1.0f);
CGContextBeginPath(context);
CGContextSetGrayFillColor(context, 0.5, 0.7);
CGContextMoveToPoint(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect));
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMinY(rect) + radius, radius, 3 * M_PI / 2, 0, 0);
CGContextAddArc(context, CGRectGetMaxX(rect) - radius, CGRectGetMaxY(rect) - radius, radius, 0, M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMaxY(rect) - radius, radius, M_PI / 2, M_PI, 0);
CGContextAddArc(context, CGRectGetMinX(rect) + radius, CGRectGetMinY(rect) + radius, radius, M_PI, 3 * M_PI / 2, 0);
CGContextClosePath(context);
CGContextFillPath(context);
This will make your rounded rectangle for your view. You can find a complete example in the HeadsUpUI sample provided with the SDK. HTH
MBProgressHUD....
http://www.cocoadev.com/index.pl?MBProgressHUD