2013-05-28 3 views
1

Я хочу обратить полый круг, которые выглядят как на рисунке нижеDraw полый круг, разделенный на равные части

enter image description here

я получил ниже фрагмент кода, чтобы сделать полый круг

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.backgroundColor = [UIColor whiteColor]; 

     // Determine our start and stop angles for the arc (in radians) 
     startAngle = M_PI * 1.5; 
     endAngle = startAngle + (M_PI * 2); 

    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    // Display our percentage as a string 
    NSString* textContent = [NSString stringWithFormat:@"%d", self.percent]; 

    UIBezierPath* bezierPath = [UIBezierPath bezierPath]; 

    // Create our arc, with the correct angles 
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width/2, rect.size.height/2) 
          radius:130 
         startAngle:startAngle 
         endAngle:(endAngle - startAngle) * (_percent/100.0) + startAngle 
         clockwise:YES]; 


    // Set the display for the path, and stroke it 
    bezierPath.lineWidth = 50; 
    [[UIColor redColor] setStroke]; 
    [bezierPath stroke]; 

    // Text Drawing 
    CGRect textRect = CGRectMake((rect.size.width/2.0) - 71/2.0, (rect.size.height/2.0) - 45/2.0, 71, 45); 
    [[UIColor blackColor] setFill]; 
    [textContent drawInRect: textRect withFont: [UIFont fontWithName: @"Helvetica-Bold" size: 42.5] lineBreakMode: NSLineBreakByWordWrapping alignment: NSTextAlignmentCenter]; 
} 

мне нужно линии разделителя также в полом круге, как в приведенном выше изображении. Любое предложение будет полезно ....

+0

http://stackoverflow.com/questions/11281811/drawing-circle-in-objective-c – Rushabh

ответ

3

Посмотрите на HKCircularProgressView. Я использую его для своего собственного проекта, расширение функциональности для ваших нужд должно быть легким.

+0

Спасибо Nenad за код Я попробую решение ... – karthik

+0

Я пробовал код, он подходит лучше всего моя потребность. Я нашел одну проблему с кодом, когда я даю текущее значение <7, один блок в круговом пропуске отсутствует. Я загрузил снимок экрана здесь http://www1.datafilehost.com/d/87da4f50 ... вы столкнулись с такой же проблемой? – karthik

+0

Хм, к сожалению, я не могу взглянуть на скриншот здесь в моей рабочей области, потому что брандмауэр блокирует сайт хостинга. На самом деле я не сталкивался с серьезными проблемами с HKCircularProgressView. Если вы намереваетесь использовать код, я бы предложил повторно разместить проблему в репозитории github HKCircularProgressView. –

0

Не отвечая на то, что было предложено, но простое решение для разделения любого круга в три равные части, решенных мой

цель исследования
- (void)drawRect:(CGRect)rect { 

CGPoint center = CGPointMake(CGRectGetWidth(self.bounds)/2.f, CGRectGetHeight(self.bounds)/2.f); 
CGFloat radius = 8.f; 

UIBezierPath *portionPath1 = [UIBezierPath bezierPath]; 
[portionPath1 moveToPoint:center]; 
[portionPath1 addArcWithCenter:center radius:radius startAngle:radians(0) endAngle:radians(120) clockwise:YES]; 
[portionPath1 closePath]; 

[[UIColor greenColor] setFill]; 
[portionPath1 fill]; 

UIBezierPath *portionPath2 = [UIBezierPath bezierPath]; 
[portionPath2 moveToPoint:center]; 
[portionPath2 addArcWithCenter:center radius:radius startAngle:radians(120) endAngle:radians(240) clockwise:YES]; 
[portionPath2 closePath]; 

[[UIColor yellowColor] setFill]; 
[portionPath2 fill]; 

UIBezierPath *portionPath3 = [UIBezierPath bezierPath]; 
[portionPath3 moveToPoint:center]; 
[portionPath3 addArcWithCenter:center radius:radius startAngle:radians(240) endAngle:radians(360) clockwise:YES]; 
[portionPath3 closePath]; 

[[UIColor blueColor] setFill]; 
[portionPath3 fill]; } 

На вершине эти две линии.

#define PI 3.14159265358979323846 
static inline float radians(double degrees) { return degrees * PI/180; } 

Надеюсь, это тоже поможет кому-то еще. :)