Reputation: 125
I have a requirement where I have to draw a Circle with a given radius. For example, say 200 meters for CLLocationManager
.
CLLocationDistance radius = 100.0;
CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:center radius:radius identifier:@"Apple"];
How do I draw the circle Using CGpoint
? I am not using any map.
Upvotes: 1
Views: 270
Reputation: 17535
You can draw like this and change it according to you what you wanna.
And you need to import QuartzCore.framework
for it
int radius = 100;
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure the apperence of the circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 5;
// Add to parent layer
[self.view.layer addSublayer:circle];
Upvotes: 0
Reputation: 437882
There are at least two approaches:
Add QuartzCore.framework to your project, create a UIBezierPath
, then create CAShapeLayer
specifying its path, and then add the CAShapeLayer
as a sublayer of your current view's layer. For example, I could call this from my view controller:
#import <QuartzCore/QuartzCore.h>
- (void)addCircle
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(self.view.layer.bounds.size.width / 2.0, self.view.layer.bounds.size.height / 2.0) radius:self.view.layer.bounds.size.width * 0.40 startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = [path CGPath];
layer.strokeColor = [[UIColor darkGrayColor] CGColor];
layer.fillColor = [[UIColor lightGrayColor] CGColor];
layer.lineWidth = 3.0;
[self.view.layer addSublayer:layer];
}
Subclass UIView
and override drawRect
to draw the circle with Core Graphics.
@implementation CircleView
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, self.bounds.size.width / 2.0, self.bounds.size.height / 2.0, self.bounds.size.width * 0.40, 0, M_PI * 2.0, YES);
CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextSetFillColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextSetLineWidth(context, 3.0);
CGContextDrawPath(context, kCGPathFillStroke);
}
@end
Both are valid. If you're not used to subclassing UIView
, the former technique is probably easier.
The Quartz 2D Programming Guide
Upvotes: 1