Tapで爆発するようなエフェクトのメモ

( Xcodeで iOS Simulator 6を使って試しています。)

ポイント

 画面のタップを起点にして、

 火花には、NSTimerのリピート

 バクハツは、dispatch_afterを使っています。

サンプルコード

( ※ViewController.mのみで完結 )

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    NSTimer *timer;

}

@property (nonatomic, strong) UIView *fire;

@end

@implementation ViewController

@synthesize fire;

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    

    //

    UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    parent.center = self.view.center;

    [self.view addSubview:parent];

    

    //

    UIView *ball = [[UIView alloc] initWithFrame:CGRectMake(15, 20, 70, 70)];

    ball.backgroundColor = [UIColor blackColor];

    ball.layer.cornerRadius = 35;

    [parent addSubview:ball];

    

    //

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(48, 0, 5, 20)];

    line.backgroundColor = [UIColor brownColor];

    [parent addSubview:line];

    

    

    // タップジェスチャーを追加

    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(start:)];

    [parent addGestureRecognizer:tgr];

    

}

// タップ

– (void)start:(UITapGestureRecognizer*)tgr

{

    // 火花

    fire = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

    fire.backgroundColor = [UIColor redColor];

    fire.alpha = 1;

    float x = self.view.center.x;

    float y = self.view.center.y50;

    fire.center = CGPointMake(x, y);

    [self.view addSubview:fire];

    

    // タイマーセット

    timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(firework) userInfo:nil repeats:YES];

    

    

    // 3秒後にボーン

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{

        [timer invalidate];

        

        [self bomb];

    });

}

// 導火線が燃えるエフェクト

– (void)firework

{

    float x = fire.center.x;

    float y = fire.center.y;

    CGPoint directions[] =

    {

        CGPointMake(x – 10, y – 20),

        CGPointMake(x     , y – 20),

        CGPointMake(x + 10, y – 20),

    };

 

    

    CGPoint randomPoint = directions[arc4random() % 3];

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];

    v.center = fire.center;

    v.backgroundColor = [UIColor redColor];

    [self.view addSubview:v];

    

    [UIView animateWithDuration:0.3 animations:^{

        v.center = randomPoint;

    } completion:^(BOOL finished) {

        [v removeFromSuperview];

    }];

}

// 爆発するエフェクト

– (void)bomb

{

    UIView *light = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    light.backgroundColor = [UIColor redColor];

    light.center = self.view.center;

    light.layer.cornerRadius = 10;

    [self.view addSubview:light];

    

    UIView *light1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];

    light1.backgroundColor = [UIColor orangeColor];

    light1.center = [self.view convertPoint:light.center toView:light];

    light1.layer.cornerRadius = 10;

    [light addSubview:light1];

    

    UIView *light2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    light2.backgroundColor = [UIColor redColor];

    light2.center = [self.view convertPoint:light.center toView:light];

    light2.layer.cornerRadius = 10;

    [light addSubview:light2];

    

    UILabel *label = [[UILabel alloc] init];

    label.text = @”Time Up!”;

    label.backgroundColor = [UIColor clearColor];

    label.font = [UIFont fontWithName:@”Chalkduster” size:20];

    [label sizeToFit];

    label.center = [self.view convertPoint:light.center toView:light];

    [light addSubview:label];

    

    [UIView animateWithDuration:0.2 animations:^{

        light.transform = CGAffineTransformMakeScale(0.8, 0.4);

    } completion:^(BOOL finished) {

        

        [UIView animateWithDuration:0.4 animations:^{

            light.transform = CGAffineTransformMakeScale(3.0, 3.0);

        }];

    }];

}

@end