2014-09-30 28 views
3

我想在Objective-c中绘制一个阴阳,我很难搞清楚如何去做。我创建了两个连接的矩形。我无法弄清楚如何制作矩形曲线的线条,这样我就可以制作一个圆圈。这是我的代码到目前为止。在我的矩形中添加一条曲线

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextAddRect(context, CGRectMake(60, 150, 80, 10)); 
CGContextStrokePath(context); 

CGContextAddRect(context, CGRectMake(60, 150, 190, 10)); 
CGContextStrokePath(context); 

有没有一种更简单的方法来创建一个阴阳在objective-c或我在正确的方向吗?一般来说,我对objective-c和编程是新手。

回答

5

我建议用圆弧(实际上是一系列半圆)来代替圆角矩形。例如,考虑一个部分是三个独立的半圆:

yin yang strokes

然后,您可以借鉴这些弧像这样:

CGContextAddArc(context, center.x, center.y - side/4.0, side/4.0, M_PI_2, -M_PI_2, TRUE); // red part 
CGContextAddArc(context, center.x, center.y + side/4.0, side/4.0, M_PI_2, -M_PI_2, NO); // blue part 
CGContextAddArc(context, center.x, center.y,    side/2.0, -M_PI_2, M_PI_2, YES); // dark grey stroke 

但很明显,你可能不会实际绘制这些笔画,而是只需填写他们,然后重复这个过程符号的底部部分

- (void)drawRect:(CGRect)rect { 
    CGFloat side = MIN(rect.size.width, rect.size.height);      // length of the side of the square in which the symbol will rest 
    CGPoint center = CGPointMake(rect.size.width/2.0, rect.size.height/2.0); // the center of that square 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // draw white part 

    CGContextAddArc(context, center.x, center.y - side/4.0, side/4.0, M_PI_2, -M_PI_2, TRUE); 
    CGContextAddArc(context, center.x, center.y + side/4.0, side/4.0, M_PI_2, -M_PI_2, NO); 
    CGContextAddArc(context, center.x, center.y,    side/2.0, -M_PI_2, M_PI_2, YES); 
    CGContextClosePath(context); 

    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
    CGContextFillPath(context); 

    // draw black part 

    CGContextAddArc(context, center.x, center.y - side/4.0, side/4.0, M_PI_2, -M_PI_2, TRUE); 
    CGContextAddArc(context, center.x, center.y + side/4.0, side/4.0, M_PI_2, -M_PI_2, NO); 
    CGContextAddArc(context, center.x, center.y,    side/2.0, -M_PI_2, M_PI_2, NO); 
    CGContextClosePath(context); 

    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextFillPath(context); 

    // draw black dot 

    CGContextAddArc(context, center.x, center.y - side/4.0, side/12.0, 0, M_PI * 2.0, YES); 
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); 
    CGContextFillPath(context); 

    // draw white dot 

    CGContextAddArc(context, center.x, center.y + side/4.0, side/12.0, 0, M_PI * 2.0, YES); 
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
    CGContextFillPath(context); 
} 

国债收益率:

yin yang

+0

谢谢!这工作完美。我一直在做错事。 – Cory 2014-09-30 19:04:03