棒人間がスクワットするようにプログラム
(XcodeのiOS6 iPhone Simulatorで動かしています。)
概要
スクワットしている棒が色とりどりに散らばるようにして、
何ともいえない意味の無さを広げていこうと思って作成。
ポイント
UIViewの中でNSTimerを設定、頭の座標をタイマーで動かす
座標は、腰まで頭と連動させ、膝以下は固定にする。
タッチで、ランダムな配色の棒人間を増やしていく。
サンプルコード
#import “ViewController.h”
@interface Sticker : UIView {
CGPoint head, neck;
CGPoint elbowR, elbowL;
CGPoint handR, handL;
CGPoint waist;
CGPoint kneeR, kneeL;
CGPoint footR, footL;
NSTimer *timer;
int counter;
}
@property (nonatomic, strong) UIColor *color;
– (void)start;
@end
@implementation Sticker
@synthesize color;
– (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
CGSize size = self.bounds.size;
head = CGPointMake(size.width * 0.5, size.height * 0.1);
neck = CGPointMake(size.width * 0.5, size.height * 0.3);
elbowL = CGPointMake(size.width * 0.2, size.height * 0.2);
handL = CGPointMake(size.width * 0.38, size.height * 0.1);
elbowR = CGPointMake(size.width * 0.8, size.height * 0.2);
handR = CGPointMake(size.width * 0.62, size.height * 0.1);
waist = CGPointMake(size.width * 0.5, size.height * 0.6);
kneeL = CGPointMake(size.width * 0.3, size.height * 0.75);
footL = CGPointMake(size.width * 0.3, size.height * 1.0);
kneeR = CGPointMake(size.width * 0.7, size.height * 0.75);
footR = CGPointMake(size.width * 0.7, size.height * 1.0);
}
return self;
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(squat) userInfo:nil repeats:YES];
}
– (void)squat
{
if (counter > 60) {
counter = 0;
}
counter++;
CGSize size = self.bounds.size;
float dh = 0.01 * (counter > 30 ? 60 – counter : counter);
head = CGPointMake(size.width * 0.5, size.height * 0.1 + size.height * dh);
neck = CGPointMake(head.x, head.y + size.height * 0.2);
elbowL = CGPointMake(neck.x – size.width * 0.2, neck.y – size.height * 0.1);
handL = CGPointMake(elbowL.x + size.width * 0.1, elbowL.y – size.height * 0.1);
elbowR = CGPointMake(neck.x + size.width * 0.2, neck.y – size.height * 0.1);
handR = CGPointMake(elbowR.x – size.width * 0.1, elbowL.y – size.height * 0.1);
waist = CGPointMake(head.x, head.y + size.height * 0.5);
kneeL = CGPointMake(size.width * 0.3, size.height * 0.75);
footL = CGPointMake(size.width * 0.3, size.height * 1.0);
kneeR = CGPointMake(size.width * 0.7, size.height * 0.75);
footR = CGPointMake(size.width * 0.7, size.height * 1.0);
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
[color set];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 5.0);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextClearRect(ctx, self.bounds);
CGContextFillEllipseInRect(ctx, CGRectMake(head.x – 10, head.y – 10, 20, 20));
CGContextMoveToPoint(ctx, neck.x, neck.y);
CGContextAddLineToPoint(ctx, elbowR.x, elbowR.y);
CGContextAddLineToPoint(ctx, handR.x, handR.y);
CGContextMoveToPoint(ctx, neck.x, neck.y);
CGContextAddLineToPoint(ctx, elbowL.x, elbowL.y);
CGContextAddLineToPoint(ctx, handL.x, handL.y);
CGContextMoveToPoint(ctx, head.x, head.y);
CGContextAddLineToPoint(ctx, waist.x, waist.y);
CGContextMoveToPoint(ctx, waist.x, waist.y);
CGContextAddLineToPoint(ctx, kneeR.x, kneeR.y);
CGContextAddLineToPoint(ctx, footR.x, footR.y);
CGContextMoveToPoint(ctx, waist.x, waist.y);
CGContextAddLineToPoint(ctx, kneeL.x, kneeL.y);
CGContextAddLineToPoint(ctx, footL.x, footL.y);
CGContextStrokePath(ctx);
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
Sticker *s = [[Sticker alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:s];
[s start];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
int hue = arc4random() % 10;
UIColor *color = [UIColor colorWithHue:hue/10.0 saturation:1.0 brightness:1.0 alpha:1.0];
Sticker *s = [[Sticker alloc] initWithFrame:CGRectMake(p.x, p.y, 100, 100)];
s.color = color;
[s start];
[self.view addSubview:s];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end