水玉模様をプチプチ壊していくiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
[self createPolkadot];
}
– (void)createPolkadot
{
for (int i=0; i<60; i++) {
float x = (i % 5) * 50.0 + (((i/5 % 2) == 0) ? 25.0 : 50.0);
float y = (i / 5) * 50.0 / 1.618 + 50;
UIColor *color = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(x, y, 30, 30)];
dot.backgroundColor = [UIColor clearColor];
dot.layer.cornerRadius = 15;
dot.layer.borderColor = color.CGColor;
dot.layer.borderWidth = 5.1;
[self.view addSubview:dot];
UIView *small = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
small.layer.cornerRadius = 10;
small.backgroundColor = color;
small.center = CGPointMake(CGRectGetWidth(dot.frame)/2.0, CGRectGetHeight(dot.frame)/2.0);
[dot addSubview:small];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[dot addGestureRecognizer:tap];
}
}
– (void)tap:(UITapGestureRecognizer*)gr
{
UIView *small = gr.view.subviews[0];
if (small) {
[UIView animateWithDuration:0.5 animations:^{
small.transform = CGAffineTransformMakeScale(0.1, 0.1);
} completion:^(BOOL finished) {
[small removeFromSuperview];
}];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end