iPhone電光石火

電光石火という文字をパパッと表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic) float delay;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:0.15 alpha:1];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self createKanji];

}

– (void)createKanji

{

    CGPoint oa = CGPointMake(160, 100);

    

    // 電

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y45) length:70 angle:0];

    [self drawLineAtPoint:CGPointMake(oa.x40, oa.y15) length:30 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y30) length:80 angle:0];

    [self drawLineAtPoint:CGPointMake(oa.x+40, oa.y15) length:30 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oa.x20, oa.y20) length:15 angle:M_PI/5.0];

    [self drawLineAtPoint:CGPointMake(oa.x20, oa.y10) length:15 angle:M_PI/5.0];

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y20) length:40 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oa.x+20, oa.y20) length:15 angle:M_PI/5.0];

    [self drawLineAtPoint:CGPointMake(oa.x+20, oa.y10) length:15 angle:M_PI/5.0];

    

    [self drawLineAtPoint:CGPointMake(oa.x25, oa.y + 20) length:30 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y + 8) length:45 angle:0];

    [self drawLineAtPoint:CGPointMake(oa.x+25, oa.y + 20) length:30 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y + 20) length:45 angle:0];

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y + 32) length:45 angle:0];

    [self drawLineAtPoint:CGPointMake(oa.x, oa.y + 30) length:45 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oa.x+20, oa.y + 50) length:40 angle:0];

    [self drawLineAtPoint:CGPointMake(oa.x+37, oa.y + 40) length:15 angle:-M_PI/2.0];

    

    //

    CGPoint ob = CGPointMake(160, 200);

    [self drawLineAtPoint:CGPointMake(ob.x, ob.y20) length:35 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(ob.x20, ob.y20) length:20 angle:M_PI/4.0];

    [self drawLineAtPoint:CGPointMake(ob.x+20, ob.y20) length:20 angle:-M_PI/4.0];

    [self drawLineAtPoint:CGPointMake(ob.x, ob.y) length:70 angle:0];

    [self drawLineAtPoint:CGPointMake(ob.x20, ob.y + 20) length:40 angle:-M_PI/3.0];

    [self drawLineAtPoint:CGPointMake(ob.x+10, ob.y + 20) length:35 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(ob.x+25, ob.y + 35) length:25 angle:0];

    

    //

    CGPoint oc = CGPointMake(160, 300);

    [self drawLineAtPoint:CGPointMake(oc.x, oc.y30) length:60 angle:0];

    [self drawLineAtPoint:CGPointMake(oc.x20, oc.y10) length:50 angle:-M_PI/3.0];

    [self drawLineAtPoint:CGPointMake(oc.x25, oc.y + 20) length:30 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oc.x, oc.y + 8) length:45 angle:0];

    [self drawLineAtPoint:CGPointMake(oc.x+25, oc.y + 20) length:30 angle:M_PI/2.0];

    [self drawLineAtPoint:CGPointMake(oc.x, oc.y + 32) length:45 angle:0];

    

    //

    CGPoint od = CGPointMake(160, 400);

    [self drawLineAtPoint:CGPointMake(od.x5, od.y) length:80 angle:-M_PI/2.5];

    [self drawLineAtPoint:CGPointMake(od.x+20, od.y + 20) length:50 angle:M_PI/4.0];

    [self drawLineAtPoint:CGPointMake(od.x20, od.y20) length:20 angle:M_PI/4.0];

    [self drawLineAtPoint:CGPointMake(od.x+20, od.y20) length:20 angle:-M_PI/4.0];

}

– (void)drawLineAtPoint:(CGPoint)p length:(float)l angle:(float)a

{

    float ox = 600 * cos(a) + p.x;

    float oy = 600 * sin(a) + p.y;

    

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

    line.transform = CGAffineTransformMakeRotation(a);

    line.center = CGPointMake(ox, oy);

    line.backgroundColor = [UIColor blackColor];

    line.layer.shadowColor = [UIColor yellowColor].CGColor;

    line.layer.shadowOpacity = 1.0;

    line.layer.shadowRadius = 3.0;

    line.layer.shadowOffset = CGSizeMake(2, 1);

    [self.view addSubview:line];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [UIView animateWithDuration:0.5 animations:^{

            line.center = p;

        }];

    });

    

    self.delay += 0.04;

}

@end