段ボール箱買いしたみかん、カビるんるんにやられてなんこか駄目になってしまった..そんな思いでiPhoneアプリのサンプルコードを描いてみました。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
typedef enum int8_t {
MoldLevel0,
MoldLevel1,
MoldLevel2,
MoldLevel3,
} MoldLevel;
@interface ViewController ()
@property NSTimeInterval time;
@property (strong, nonatomic) CATextLayer *score;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.4 alpha:1.0];
[self createScore];
[self setupManderin];
[self startTimer];
}
– (void)createScore
{
self.score = [CATextLayer layer];
self.score.name = @”score”;
self.score.frame = CGRectMake(80, 40, 200, 50);
self.score.string = @”000000″;
self.score.font = (__bridge CFTypeRef)@”ChalkboardSE-Bold”;
self.score.fontSize = 40;
self.score.foregroundColor = [UIColor orangeColor].CGColor;
[self.view.layer addSublayer:self.score];
}
– (void)setupManderin
{
UIImage *mandarin = [UIImage imageNamed:@”mandarin”];
for (int i=0; i<42; i++) {
float x = (i%6) * 50 + 10;
float y = (i/6) * 50 + 110;
UIImageView *mandarinView = [[UIImageView alloc] initWithImage:mandarin];
mandarinView.tag = MoldLevel0;
mandarinView.frame = CGRectMake(x, y, 50, 50);
[self.view addSubview:mandarinView];
}
}
– (void)startTimer
{
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
if (self.time > 1) {
[self propagation];
[self moldy];
self.time = 0;
}
self.time += sender.timeInterval;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”tag == %d”, MoldLevel0];
NSArray *cleanMandarin = [self.view.subviews filteredArrayUsingPredicate:predicate];
if (cleanMandarin.count == 0) {
[sender invalidate];
}
}
– (void)moldy
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”tag == %d”, MoldLevel0];
NSArray *seeds = [self.view.subviews filteredArrayUsingPredicate:predicate];
int rand = arc4random() % (seeds.count);
UIView *target = [seeds objectAtIndex:rand];
UIImage *moldImg = [UIImage imageNamed:@”mold01″];
UIImageView *mold = [[UIImageView alloc] initWithImage:moldImg];
mold.center = CGPointMake(25, 25);
[target addSubview:mold];
target.tag = MoldLevel1;
}
– (void)propagation
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”tag > %d”, MoldLevel0];
NSArray *moldyFoods = [self.view.subviews filteredArrayUsingPredicate:predicate];
for (UIView *v in moldyFoods) {
if (v.tag == MoldLevel3) {
[UIView animateWithDuration:0.5 animations:^{
v.alpha = 0;
} completion:^(BOOL finished) {
[v removeFromSuperview];
}];
} else if (v.tag == MoldLevel2) {
UIImage *img = [UIImage imageNamed:@”mold03″];
UIImageView *mold = [[UIImageView alloc] initWithImage:img];
mold.center = CGPointMake(25, 25);
[v addSubview:mold];
v.tag = MoldLevel3;
} else if (v.tag == MoldLevel1) {
UIImage *img = [UIImage imageNamed:@”mold02″];
UIImageView *mold = [[UIImageView alloc] initWithImage:img];
mold.center = CGPointMake(25, 25);
[v addSubview:mold];
v.tag = MoldLevel2;
}
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@”tag == %d”, MoldLevel0];
NSArray *cleanMandarin = [self.view.subviews filteredArrayUsingPredicate:predicate];
for (UIView *v in cleanMandarin) {
if (CGRectContainsPoint(v.frame, p)) {
v.tag = 10;
[UIView animateWithDuration:0.2 animations:^{
v.transform = CGAffineTransformMakeScale(1.5, 1.5);
} completion:^(BOOL finished) {
[v removeFromSuperview];
}];
// score up
self.score.string = [NSString stringWithFormat:@”%06d”, [self.score.string intValue] + 40];
}
}
}
@end