在iOS上的圆内绘制曲线矩形 [英] Draw a Curvilinear Rectangle inside a circle on iOS

查看:115
本文介绍了在iOS上的圆内绘制曲线矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个分集轮(内部带有曲线矩形的圆),请参见:

I'm trying to draw a diversity wheel (a circle with curvilinear rectangles inside) see:

圆http://api.ning.com/files/oKZwndeqeam7%2aMiO7f4BDUT%2aAgw3WsK3kW- b-wXjR8gCrCqVAv3RpyBAdi%2adYSLaca0kAYCY0Wk13bSHDnEbOVR1NNUuYotV / diversity_wheel3.JPG?width = 500

我知道如何绘制圆,但是我不知道如何绘制圆基于圆坐标的曲线矩形。

I know how to draw a circle, but I don't know how to draw the curvilinear rectangle based on the circle coordinates.

我该怎么做?

推荐答案

因为您知道如何绘制圆,只需在其中心添加一些线,您将得到与发布的图像类似的图像:

Since you know how to draw circles, just add some lines to its center and you get something like the image you posted:

- (void)drawRect:(CGRect)rect
{
    CGPoint centerPoint = self.center;
    CGFloat circleWidth = 30;
    int numCircles = 4;

    [[UIColor colorWithHue:0.53 saturation:1 brightness:0.6 alpha:1] setStroke];

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    for(int i=numCircles-1;i>=0;i--){
        //calculate some color
        CGFloat colorModifier = ((numCircles-i)/(float)numCircles);
        [[UIColor colorWithHue:0.53 saturation:colorModifier*0.8+0.2 brightness:1-colorModifier*0.4 alpha:1] setFill];

        CGFloat radius = circleWidth*(i+1);

        //draw the circle
        CGContextFillEllipseInRect(ctx, CGRectMake(centerPoint.x-radius, centerPoint.y-radius, 2*radius, 2*radius));
        CGContextStrokeEllipseInRect(ctx, CGRectMake(centerPoint.x-radius, centerPoint.y-radius, 2*radius, 2*radius));

        if(i>0){
            //just add a random number of dividers here
            int numDivider = 3+(arc4random()%5);
            float angleStep = 2*M_PI/numDivider;
            for(int j=0;j<numDivider;j++){
                CGFloat x = centerPoint.x + sinf(j*angleStep)*radius;
                CGFloat y = centerPoint.y + cosf(j*angleStep)*radius;
                CGContextMoveToPoint(ctx, centerPoint.x, centerPoint.y);
                CGContextAddLineToPoint(ctx, x, y);
                CGContextStrokePath(ctx);
            }
        }
    }
}

A仅绘制单个弯曲矩形的可能性是绘制圆弧并擦除内部圆。像这样:

A possibility to just draw a single curved rectangle would be to draw an arc and erase an inner circle. Like this:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:self.center];
    [path addArcWithCenter:self.center radius:150 startAngle:-0.3 endAngle:0.3 clockwise:YES];
    [path fill];

    UIBezierPath *innerPath = [UIBezierPath bezierPath];
    [innerPath moveToPoint:self.center];
    [innerPath addArcWithCenter:self.center radius:120 startAngle:0 endAngle:2*M_PI clockwise:YES];
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    [innerPath fill];

这篇关于在iOS上的圆内绘制曲线矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆