四角いドットを使って、2色をグラデーションっぽく表示、ボタンで色を切り替え可能。という感じでiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property (strong, nonatomic) UIColor *color1;
@property (strong, nonatomic) UIColor *color2;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.color1 = [UIColor yellowColor];
self.color2 = [UIColor greenColor];
self.view.backgroundColor = self.color1;
UIView *half = [[UIView alloc] initWithFrame:CGRectMake(0, 274, 320, 274)];
half.backgroundColor = self.color2;
[self.view addSubview:half];
[self gradationDot];
[self createPallet];
}
– (void)gradationDot
{
// color2 dot
for (int i=0; i<100; i++) {
float x = (i % 20) * 30 + 10;
float y = (i / 20) * 30 + 150;
float s = (i / 10) + 10;
UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, s, s)];
dot.center = CGPointMake(x, y);
dot.backgroundColor = self.color2;
[self.view addSubview:dot];
}
// color1 dot
for (int i=0; i<100; i++) {
float x = (i % 20) * 30 + 10;
float y = (i / 20) * 30 + 285;
float s = 19 – (i / 10);
UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, s, s)];
dot.center = CGPointMake(x, y);
dot.backgroundColor = self.color1;
[self.view addSubview:dot];
}
}
– (void)createPallet
{
UIView *pallet = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
pallet.backgroundColor = [UIColor whiteColor];
[self.view addSubview:pallet];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(5, 5, 30, 30);
[pallet addSubview:btn];
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
}
– (void)tap:(UIButton*)btn
{
int counter = 0;
UIColor *color = btn.backgroundColor;
for (UIView *sameColor in self.view.subviews) {
if ([sameColor.backgroundColor isEqual:self.view.backgroundColor]) {
dispatch_queue_t myQ = dispatch_queue_create(“change color”, NULL);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.005 * counter* NSEC_PER_SEC), myQ, ^{
dispatch_async(dispatch_get_main_queue(), ^{
sameColor.backgroundColor = color;
});
});
counter++;
}
}
self.view.backgroundColor = color;
btn.backgroundColor = [UIColor colorWithHue:(arc4random()%10) * 0.1 saturation:1.0 brightness:1.0 alpha:1.0];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end