2013-06-24 27 views
6

Ben bir acemi ios programcısıyım. Projemden birinde, çemberin farklı bölümlerinin farklı renklerle doldurulacağı bir çember çizmem gerekiyor. circle.But çemberin farklı bölümünü belirleyemiyorum ve onları farklı bir renkle doldurmam mümkün değil. Ne çizmek istediğimi açıklamak için bir ekran görüntüsü. circle with different colorFarklı renklerle dolu bir daire çizimi, farklı bir renkle doldurun

Bir yardım mutluluk duyacaktır.

+0

bir pasta grafiği benziyor. Bunun için neden [Çekirdek Arsa] (http://www.raywenderlich.com/13269/how-to-draw-graphs-with-core-plot-part-1) kullanılmıyor? – Amar

+0

İnsanların uygulamanıza uygun yanıtları verebilmesi için, CoreGraphics işlevlerini ("CGContext" ile başlayan işlevler) kendi "drawRect" inizde mi yoksa "UIBezierPath" komutunu kullanarak bize bildirin. "strok" yöntemi veya "CAShapeLayer" kullanımı. – Rob

+0

@Rob aslında CGContext gibi temel grafik işlevlerini kullanıyorum. – sujat

cevap

11

UIBezierPath'u kullanarak, yarıçapı, merkezi ve açıları belirtebileceğiniz bir yöntem olan addArcWithCenter:radius:startAngle:endAngle:clockwise: kullanabilirsiniz. Kod yeşil bir dairenin dörtte çizer hangi aşağıdaki gibi görünebilir:

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

UIBezierPath *portionPath = [UIBezierPath bezierPath]; 
[portionPath moveToPoint:center]; 
[portionPath addArcWithCenter:center radius:radius startAngle:0.f endAngle:M_PI_2 clockwise:YES]; 
[portionPath closePath]; 

[[UIColor greenColor] setFill]; 
[portionPath fill]; 

UIBezierPath *portionPath1 = [UIBezierPath bezierPath]; 
[portionPath1 moveToPoint:center]; 
[portionPath1 addArcWithCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES]; 
[portionPath1 closePath]; 

[[UIColor blueColor] setFill]; 
[portionPath1 fill]; 

Tabii siz de CorePlot gibi bir kütüphane kullanmayı düşünebilirsiniz.

+0

bir porsiyon için mükemmel çalışabilir.but Diğer bölümleri ile nasıl devam edebilirim? Başlangıç ​​ve açıyı değiştiren diğer bölümler için de aynı işlemi yaptım. ama arzu edilmeyen şekilde çalışıyor – sujat

+0

Sadece açı ve renklerle oyna. Merkez ve yarıçap her zaman aynı olmalıdır. Her bölümün açısını hesaplamanız ve daha sonra başlangıç ​​ve devam edenleri artıran tüm bölümler arasında geçiş yapmanız gerekir. – Karl

+0

Bunu da düşündüm. Ama benim açım hakkında yanlış olan bir şey var. Bana sadece başka bir bölüm açısı hesaplaması gösterebilir misiniz. Bir sonraki bölüm için bunu denedim [startAngle: M_PI_2 endAngle: M_PI] – sujat

0

Bu basit bir trigonometri sorusu. Pasta kısmı için gereken açıyı hesaplayın (pasta dilimi yüzdesi 360 derece), sonra merkeze hareket edin. Uygun bir açıda daire kenarına doğru ilerleyin, ihtiyacınız olan yay açısı için pasta diliminin bir sonraki tarafına doğru ilerleyin, ardından merkeze geri gelin.

Alternatif olarak, sizin için pasta grafikler yapmak için CorePlot kullanabilirsiniz.

2

Elimizdeki 6 sınıf

CircleViewController.h, CircleViewController.m, GraphView.h, GraphView.h, CirclePart.h, CirclePart.m

CirclePart.h

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 

@interface CirclePart : NSObject 

@property (nonatomic) CGFloat startDegree; 
@property (nonatomic) CGFloat endDegree; 
@property (nonatomic) UIColor *partColor; 

-(id)initWithStartDegree:(CGFloat)startDegree endDegree:(CGFloat)endDegree partColor:(UIColor*)partColor; 

@end 

CirclePart.m

#import "CirclePart.h" 

@implementation CirclePart 

-(id)initWithStartDegree:(CGFloat)startDegree endDegree:(CGFloat)endDegree partColor:(UIColor*)partColor 
{ 
    self = [super init]; 
    if (self) { 
     self.startDegree = startDegree; 
     self.endDegree = endDegree; 
     self.partColor = partColor; 
    } 
    return self; 
} 

@end 

GraphView.h

#import <UIKit/UIKit.h> 
#import "CirclePart.h" 

@interface GraphView : UIView 

@property (nonatomic) CGPoint centrePoint; 
@property (nonatomic) CGFloat radius; 
@property (nonatomic) CGFloat lineWidth; 
@property (nonatomic, strong) NSArray *circleParts; 

-(id)initWithFrame:(CGRect)frame CentrePoint:(CGPoint)centrePoint radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth circleParts:(NSArray*)circleParts; 

@end 

GraphView.m

#import "GraphView.h" 

@implementation GraphView 

-(id)initWithFrame:(CGRect)frame CentrePoint:(CGPoint)centrePoint radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth circleParts:(NSArray*)circleParts 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.centrePoint = centrePoint; 
     self.radius = radius; 
     self.lineWidth = lineWidth; 
     self.circleParts = circleParts; 
    } 
    return self; 
} 

- (void)drawRect:(CGRect)rect { 

    [self drawCircle]; 
} 

- (void)drawCircle { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, _lineWidth); 

    for(CirclePart *circlePart in _circleParts) 
    { 
     CGContextMoveToPoint(context, _centrePoint.x, _centrePoint.y); 

     CGContextAddArc(context, _centrePoint.x , _centrePoint.y, _radius, [self radians:circlePart.startDegree], [self radians:circlePart.endDegree], 0); 
     CGContextSetFillColorWithColor(context, circlePart.partColor.CGColor); 
     CGContextFillPath(context); 
    } 
} 

-(float) radians:(double) degrees { 
    return degrees * M_PI/180; 
} 


@end 

CircleViewController.h

#import <UIKit/UIKit.h> 

@interface CircleViewController : UIViewController 

@end 

CircleViewController.m

#import "CircleViewController.h" 
#import "GraphView.h" 
#import "CirclePart.h" 

@interface CircleViewController() 

@end 

@implementation CircleViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CirclePart *part1 = [[CirclePart alloc] initWithStartDegree:0 endDegree:120 partColor:[UIColor redColor]]; 
    CirclePart *part2 = [[CirclePart alloc] initWithStartDegree:120 endDegree:250 partColor:[UIColor blueColor]]; 
    CirclePart *part3 = [[CirclePart alloc] initWithStartDegree:250 endDegree:360 partColor:[UIColor grayColor]]; 

    NSArray *circleParts = [[NSArray alloc] initWithObjects:part1, part2, part3, nil]; 

    CGRect rect = CGRectMake(100, 100, 200, 200); 
    CGPoint circleCenter = CGPointMake(rect.size.width/2, rect.size.height/2); 

    GraphView *graphView = [[GraphView alloc] initWithFrame:rect CentrePoint:circleCenter radius:80 lineWidth:2 circleParts:circleParts]; 
    graphView.backgroundColor = [UIColor whiteColor]; 
    graphView.layer.borderColor = [UIColor redColor].CGColor; 
    graphView.layer.borderWidth = 1.0f; 

    [self.view addSubview:graphView]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end