3つのドングリのうち、大きいのはどれでしょうか。というiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *acorns;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:1.0 green:0.6 blue:0 alpha:1.0];
UIView *base = [[UIView alloc] initWithFrame:CGRectMake(0, 280, 568, 40)];
base.backgroundColor = [UIColor colorWithRed:0.5 green:0.3 blue:0 alpha:1.0];
[self.view addSubview:base];
[self createAcorns];
}
– (void)createAcorns
{
for (int i=0; i<3; i++) {
float x = 180 * i + 100;
float y = 150;
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”acorn”]];
float w = (arc4random() % 100) + 50;
float h = (arc4random() % 100) + 119;
iv.frame = CGRectMake(0, 0, w, h);
iv.center = CGPointMake(x, y);
float r = (float)(arc4random() % 314) / 100.0;
iv.transform = CGAffineTransformMakeRotation(r);
[self.view addSubview:iv];
iv.userInteractionEnabled = YES;
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(choice:)];
[iv addGestureRecognizer:tap];
[self.acorns addObject:iv];
}
}
– (void)choice:(UITapGestureRecognizer*)gr
{
int bigIndex = 0;
float lastSize = 0;
for (int i=0; i<[self.acorns count]; i++) {
UIView *v = [self.acorns objectAtIndex:i];
if (lastSize < v.bounds.size.height) {
lastSize = v.bounds.size.height;
bigIndex = i;
};
[UIView animateWithDuration:0.3 animations:^{
float angle = [[v.layer valueForKeyPath:@”transform.rotation.z”] floatValue];
v.transform = CGAffineTransformRotate(v.transform, -angle);
}];
}
UIView *lines = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:lines];
CAShapeLayer *sl = [CAShapeLayer layer];
sl.backgroundColor = [UIColor clearColor].CGColor;
sl.strokeColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor;
sl.lineWidth = 2;
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i = 0; i<20; i++) {
float y = 250 – i * 10;
[path moveToPoint:CGPointMake(0, y)];
[path addLineToPoint:CGPointMake(568, y)];
sl.path = path.CGPath;
[lines.layer addSublayer:sl];
}
UILabel *big = [[UILabel alloc] init];
big.font = [UIFont boldSystemFontOfSize:40];
big.textColor = [UIColor redColor];
big.center = CGPointMake(800, 230);
big.text = @”BIG”;
[big sizeToFit];
[self.view addSubview:big];
[UIView animateWithDuration:1.0 animations:^{
big.center = [[self.acorns objectAtIndex:bigIndex] center];
}];
[self performSelector:@selector(restart) withObject:Nil afterDelay:5.0];
}
– (void)restart
{
for (UIView *v in self.view.subviews) {
[v removeFromSuperview];
}
self.acorns = nil;
UIView *base = [[UIView alloc] initWithFrame:CGRectMake(0, 280, 568, 40)];
base.backgroundColor = [UIColor colorWithRed:0.5 green:0.3 blue:0 alpha:1.0];
[self.view addSubview:base];
[self createAcorns];
}
– (NSMutableArray *)acorns
{
if (!_acorns) {
_acorns = [[NSMutableArray alloc] init];
}
return _acorns;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end