三角、丸、五角形などのカタチであそぶサンプル
(XcodeのiOS6 Simulatorで試しています。)
ポイント
・4つの形を表示するためのUIViewを作っておく
・大きさ、色、位置をランダムで算出するようにする。
・PanGestureで好きなように動かせるようにする
サンプルコード
#import “ViewController.h”
// 図形用のView
typedef enum {
ShapeTypeRect,
ShapeTypeCircle,
ShapeTypeTriangle,
ShapeTypePentagon,
} ShapeType;
@interface Shape : UIView
@property ShapeType stype;
@property UIColor *color;
@end
@implementation Shape
@synthesize stype, color;
– (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
– (void)drawRect:(CGRect)rect
{
[color setFill];
switch (stype) {
case ShapeTypeRect:
[self createRectShape];
break;
case ShapeTypeCircle:
[self createCircleShape];
break;
case ShapeTypeTriangle:
[self createTriangleShape];
break;
case ShapeTypePentagon:
[self createPentagonShape];
break;
}
}
– (void)createRectShape
{
// 四角
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddRect(ctx, self.bounds);
CGContextFillPath(ctx);
}
– (void)createCircleShape
{
// 円
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddArc(ctx, self.bounds.size.width * 0.5, self.bounds.size.height * 0.5, self.bounds.size.width * 0.5, 0, M_PI * 2, NO);
CGContextFillPath(ctx);
}
– (void)createTriangleShape
{
// 三角
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPoint points[] = {
CGPointMake(0,self.bounds.size.height),
CGPointMake(self.bounds.size.width * 0.5, 0),
CGPointMake(self.bounds.size.width, self.bounds.size.height)
};
CGContextAddLines(ctx, points, 3);
CGContextFillPath(ctx);
}
– (void)createPentagonShape
{
// 五角形
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 点は円上にあるからこんな感じで
float r = self.bounds.size.width * 0.5;
CGPoint o = CGPointMake(r, r);
CGPoint points[] = {
CGPointMake(r * cos(0) + o.x, o.y),
CGPointMake(r * cos(72 * (M_PI / 180.0)) + o.x, r * sin(72 * (M_PI / 180.0)) + o.y ),
CGPointMake(r * cos(144 * (M_PI / 180.0)) + o.x, r * sin(144 * (M_PI / 180.0)) + o.y ),
CGPointMake(r * cos(216 * (M_PI / 180.0)) + o.x, r * sin(216 * (M_PI / 180.0)) + o.y ),
CGPointMake(r * cos(288 * (M_PI / 180.0)) + o.x, r * sin(288 * (M_PI / 180.0)) + o.y ),
};
CGContextAddLines(ctx, points, 5);
CGContextFillPath(ctx);
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createShapes];
}
– (void)createShapes
{
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor yellowColor], nil];
for (int i=0; i<20; i++) {
ShapeType type = arc4random() % 4;
float x = arc4random() % 250 + 30;
float y = arc4random() % 100 + 320;
float size = arc4random() % 30 + 20;
Shape *s = [[Shape alloc] initWithFrame:CGRectMake(x, y, size, size)];
s.stype = type;
s.color = colors[i % [colors count]];
[self.view addSubview:s];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[s addGestureRecognizer:pan];
}
}
– (void)move:(UIGestureRecognizer*)gr
{
gr.view.center = [gr locationInView:self.view];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}