I am trying to draw a color wheel for an iPhone, but I can\'t get the gradient to rotate around a point. I am trying to use gradients but objective-c provides a linear grad
Using only UIKit methods:
// ViewController.m; assuming ViewController is the app's root view controller
#include "ViewController.h"
@interface ViewController ()
{
UIImage *img;
UIImageView *iv;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
int sectors = 180;
float radius = MIN(size.width, size.height)/2;
float angle = 2 * M_PI/sectors;
UIBezierPath *bezierPath;
for ( int i = 0; i < sectors; i++)
{
CGPoint center = CGPointMake(size.width/2, size.height/2);
bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:i * angle endAngle:(i + 1) * angle clockwise:YES];
[bezierPath addLineToPoint:center];
[bezierPath closePath];
UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
[color setFill];
[color setStroke];
[bezierPath fill];
[bezierPath stroke];
}
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
iv = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:iv];
}
@end
Basically all that the above code does is go around the circle drawing narrow sectors filling them with colors of incrementally increasing hue.
You could of course do all the drawing directly into a view's graphics context with drawRect()
and not have to create an explicit image context.