1

我有一个要求,我必须绘制一个给定半径的圆。例如,假设 200 米CLLocationManager

CLLocationDistance radius = 100.0;
CLRegion *region = [[CLRegion alloc]initCircularRegionWithCenter:center radius:radius identifier:@"Apple"];

如何使用 绘制圆圈CGpoint?我没有使用任何地图。

4

2 回答 2

1

至少有两种方法:

  1. 将 QuartzCore.framework 添加到您的项目中,创建一个UIBezierPath,然后创建CAShapeLayer指定其路径,然后将其添加CAShapeLayer为当前视图层的子层。例如,我可以从我的视图控制器中调用它:

    #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];
    }
    
  2. 子类化UIView并覆盖drawRect以使用 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
    

两者都是有效的。如果您不习惯子类UIView化,前一种技术可能更容易。

参考

Quartz 2D 编程指南

于 2013-05-16T05:02:03.133 回答
0

你可以像这样画,并根据你想要的改变它。你需要QuartzCore.framework为它导入

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];
于 2013-05-16T05:02:13.400 回答