ルート3長方形を3つ使って、六角形を作るiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (strong, nonatomic) UIView *one, *two, *three;
@property (assign, nonatomic) int counter;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self color:4];
self.one = [self createRoot3Rect];
self.one.backgroundColor = [self color:0];
[self.view addSubview:self.one];
self.two = [self createRoot3Rect];
self.two.backgroundColor = [self color:1];
[self.view addSubview:self.two];
self.three = [self createRoot3Rect];
self.three.backgroundColor = [self color:2];
[self.view addSubview:self.three];
}
– (UIView*)createRoot3Rect
{
float h = 100;
float w = h * sqrt(3.0);
UIView *rect3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];
rect3.center = CGPointMake(160, 250);
rect3.layer.borderColor = [self color:3].CGColor;
rect3.layer.borderWidth = 2;
rect3.alpha = 0.6;
return rect3;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.counter = (self.counter + 1) % 3;
[UIView animateWithDuration:0.5 animations:^{
switch (self.counter) {
case 0:
self.two.transform = CGAffineTransformIdentity;
self.three.transform = CGAffineTransformIdentity;
break;
case 1:
self.two.transform = CGAffineTransformMakeRotation(2.0*M_PI/3.0);
break;
case 2:
self.three.transform = CGAffineTransformMakeRotation(2.0 * 2.0*M_PI/3.0);
break;
default:
break;
}
}];
}
#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
– (UIColor*)color:(int)i
{
switch (i) {
case 0:
return UIColorHex(0xEA6045);
case 1:
return UIColorHex(0xF8CA4D);
case 2:
return UIColorHex(0xF5E5C0);
case 3:
return UIColorHex(0x3F5666);
case 4:
return UIColorHex(0x2F3440);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end