Drawing circle in Objective-c

后端 未结 4 457
忘掉有多难
忘掉有多难 2021-02-02 04:36

I am a beginner in iPhone programing. I need to create circles like Figure 1, where it should be divided in six different parts with four different levels (see Figure 1). Furthe

相关标签:
4条回答
  • 2021-02-02 05:03

    Take a look at the Quartz 2D Programming Guide, especially the Ellipses and the Clipping to Paths sections. The rest is just some basic math and geometry.

    You subclass UIView, use the Quartz 2D framework to draw your circles and probably implement the touchesBegan: and touchesEnded: methods to handle taps on the circles.

    0 讨论(0)
  • 2021-02-02 05:08

    Actually I think this revised answer will help you more in the long run. Take a look at the documentation for these:

    CGPathAddLineToPoint    
    CGPathAddArc
    

    This is what I used to do basically exactly what your trying to do.

    0 讨论(0)
  • 2021-02-02 05:11

    Pie Chart Example:

    int sum = 0;
    CGFloat offset;
    CGFloat angleArray[numberOfSections+1];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    for(int i=0; i<numberOfSections; i++) {
        sum += angle;
    }
    
    for(int i=0; i<numberOfSections; i++) {
        angleArray[i] = (float)((angle)/(float)sum)*(2*M_PI); 
        CGContextMoveToPoint(context, radius, radius);
    
        if(i == 0) {
            CGContextAddArc(context, radius, radius, radius, 0, angleArray[i], 0);
        } else {
            CGContextAddArc(context, radius, radius, radius, offset, (offset+angleArray[i]), 0);
        }
        offset += angleArray[i];
    
        CGContextSetFillColorWithColor(context, ((UIColor *)[hjulColorArray objectAtIndex:i]).CGColor);
        CGContextClosePath(context); 
        CGContextFillPath(context);
    }
    
    0 讨论(0)
  • If you in any way want to have the changes animate (like when you zoom in on a category in the last image) then you should really try and use as much Core Animation as possible.

    Have a look at this great tutorial on making a custom animatable pie chart with Core Animation. I'm sure that you can modify it to get what you want.

    Also, if you don't care about animations and are okay with only displaying circles that jump from state to state then Core Animation will probably just make things overly complex and thing like Core Graphics (like the other answers mentioned) is probably the right way to go.

    0 讨论(0)
提交回复
热议问题