首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Quartz CGContextFillEllipseInRect绘制两个圆

使用Quartz CGContextFillEllipseInRect绘制两个圆
EN

Stack Overflow用户
提问于 2012-03-13 20:36:46
回答 4查看 7.9K关注 0票数 6

我试着画两个圆,一个在另一个里面,如下图所示。

我已经很好地画了一个圆(外部的那个),但我不确定如何在顶部添加第二个圆,以及如何将其居中。

代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 4.0);
    CGContextSetStrokeColorWithColor(context, 
                                     [UIColor whiteColor].CGColor);
    //
     UIColor *theFillColor = UIColorFromRGB(0x6c83a6);
    CGContextSetFillColor(context, CGColorGetComponents(theFillColor.CGColor));

    CGRect rectangle = CGRectMake(5.0,5.0,rect.size.width-10.0,rect.size.height-10.0);


    CGContextAddEllipseInRect(context, rectangle);
    CGContextStrokePath(context);
    CGContextFillEllipseInRect(context, rectangle);
    UIGraphicsEndImageContext();
    //
    // INSIDE ?
    //

}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-13 20:45:46

代码语言:javascript
复制
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
UIColor *theFillColor = UIColorFromRGB(0x6c83a6);
CGContextSetFillColorWithColor(context, theFillColor.CGColor);

CGRect rectangle = CGRectMake(5.0,5.0,rect.size.width-10.0,rect.size.height-10.0);
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, rectangle);
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill

CGRect smallRect = CGRectInset(rectangle, 40, 40); // change 40 with the desired value
// You may change the fill and stroke here before drawing the circle
CGContextBeginPath(context);
CGContextAddEllipseInRect(context, smallRect);
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill

UIGraphicsEndImageContext();
票数 15
EN

Stack Overflow用户

发布于 2012-10-13 02:16:31

/*类似于前面的解决方案,但稍微简单一些。定义的半径和起点-终点角度变量仅为清晰起始点。*/

代码语言:javascript
复制
#define PI 3.14285714285714
float radius1 = 80;
float radius2 = 30;
float startAngle = 0;
float endAngle = endAngle = PI*2;
CGPoint position = CGPointMake(100,100);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
UIColor *theFillColor = UIColorFromRGB(0x6c83a6);
CGContextSetFillColorWithColor(context, theFillColor.CGColor);

CGContextBeginPath(context);
CGContextAddArc(ctx, position.x, position.y, radius1, startAngle, endAngle, 1);
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill

// You may change the fill and stroke here before drawing the circle
CGContextBeginPath(context);
CGContextAddArc(ctx, position.x, position.y, radius2, startAngle, endAngle, 1);
CGContextDrawPath(context, kCGPathFillStroke); // Or kCGPathFill

UIGraphicsEndImageContext();
票数 5
EN

Stack Overflow用户

发布于 2016-07-21 06:04:48

斯威夫特

代码语言:javascript
复制
func drawRect(rect: CGRect) {
let context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextSetLineWidth(context, 4.0)
CGContextSetStrokeColorWithColor(context, UIColor(hue: 0, saturation: 0, brightness: 94, alpha: 242).CGColor)

let theFillColor: UIColor = UIColor(hue: 0, saturation: 100, brightness: 80, alpha: 204)
CGContextSetFillColorWithColor(context, theFillColor.CGColor)

let rectangle: CGRect = CGRectMake(5.0, 5.0, rect.size.width-10.0, rect.size.height-10.0)
CGContextBeginPath(context)
CGContextAddEllipseInRect(context, rectangle)
CGContextDrawPath(context, CGPathDrawingMode.FillStroke)

let smallRect: CGRect = CGRectInset(rectangle, 40, 40)

CGContextBeginPath(context)
CGContextAddEllipseInRect(context, smallRect)
CGContextDrawPath(context, CGPathDrawingMode.FillStroke)
UIGraphicsEndImageContext()
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9684006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档