iPhone年輪

年輪

カラフルな年輪をぽちぽちと作っていくようなiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UIView *current;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(press:)];

    lpgr.minimumPressDuration = 0;

    [self.view addGestureRecognizer:lpgr];

}

– (void)press:(UILongPressGestureRecognizer*)gr

{

    if (gr.state == UIGestureRecognizerStateBegan) {

        CGPoint p = [gr locationInView:self.view];

        

        UIView *ring = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];

        ring.center = p;

        float hue = (arc4random() % 9) * 0.1 + 0.1;

        ring.backgroundColor = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0];

        ring.layer.cornerRadius = 200;

        [self.view addSubview:ring];

        

        self.current = ring;

        

        ring.transform = CGAffineTransformMakeScale(0.05, 0.05);

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0/40.0 target:self selector:@selector(grow:) userInfo:ring repeats:YES];

    }

    else if (gr.state == UIGestureRecognizerStateEnded

        || gr.state == UIGestureRecognizerStateCancelled) {

        [self.timer invalidate];

    }

}

– (void)grow:(NSTimer*)sender

{

    UIView *ring = sender.userInfo;

    ring.transform = CGAffineTransformScale(ring.transform, 1.02, 1.02);

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end