Skip to content

Instantly share code, notes, and snippets.

@johnny77221
Created October 26, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnny77221/2dd38c0fdb36c4b4262b to your computer and use it in GitHub Desktop.
Save johnny77221/2dd38c0fdb36c4b4262b to your computer and use it in GitHub Desktop.
NSArray *food = @[
@{
@"name" : @"滄州燒臘",
@"score" : @"50"
},
@{
@"name" : @"Subway",
@"score" : @"80"
},
@{
@"name" : @"鐵板城",
@"score" : @"70"
},
];
int totalScore = 0;
for (int i=0; i<[food count]; i++) {
int foodScore = [food[i][@"score"] intValue];
totalScore += foodScore;
}
CGFloat lastAngle = 0;
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
for (int i=0; i<[food count]; i++) {
CGFloat hue = (CGFloat)arc4random_uniform(256) / 256.0; // 0.0 to 1.0
CGFloat saturation = ( (CGFloat)arc4random_uniform(128) / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( (CGFloat)arc4random_uniform(128) / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
[color setFill];
int foodScore = [food[i][@"score"] intValue];
CGFloat angle = M_PI * 2 * foodScore / totalScore;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:100 startAngle:lastAngle endAngle:angle + lastAngle clockwise:YES];
[path addLineToPoint:CGPointMake(100, 100)];
[path closePath];
[path fill];
CGPoint smallerCirclePoint = CGPointMake(100 + 50 * cos(lastAngle + angle / 2), 100 + 50 * sin(lastAngle + angle / 2));
NSString *foodName = food[i][@"name"];
[foodName drawAtPoint:smallerCirclePoint withAttributes:@{
NSForegroundColorAttributeName : [UIColor blackColor]
}];
lastAngle = angle + lastAngle;
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.center = self.view.center;
[self.view addSubview:imageView];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment