iPhone落書きタイマー

落書きの上を10秒でうごくタイマーiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) CAShapeLayer *shape;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    [self createLabel];

}

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

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    

    UIView *v = [self.view hitTest:p withEvent:nil];

    if (v == self.view) {

        if (!self.shape) {

            CAShapeLayer *l = [CAShapeLayer layer];

            l.strokeColor = [UIColor blackColor].CGColor;

            l.lineWidth = 5;

            l.fillColor = [UIColor clearColor].CGColor;

            [self.view.layer addSublayer:l];

            self.shape = l;

        }

        

        UIBezierPath *path = [UIBezierPath bezierPath];

        [path moveToPoint:p];

        self.shape.path = path.CGPath;

    }

}

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

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:self.shape.path];

    [path addLineToPoint:p];

    

    self.shape.path = path.CGPath;

}

– (void)createLabel

{

    UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 100, 40)];

    l.text = @”START”;

    l.textAlignment = NSTextAlignmentCenter;

    l.textColor = [UIColor whiteColor];

    l.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];

    [self.view addSubview:l];

    

    l.userInteractionEnabled = YES;

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

    [l addGestureRecognizer:tap];

}

– (void)start:(UITapGestureRecognizer *)gr

{

    UILabel *l = (UILabel *)gr.view;

    

    [UIView animateWithDuration:0.2 animations:^{

        l.transform = CGAffineTransformMakeTranslation(-CGRectGetMaxX(gr.view.bounds), 0);

    } completion:^(BOOL finished) {

        l.text = @”10.00″;

        

        [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(updateTime:) userInfo:@{@”start”:[NSDate date], @”label”:l} repeats:YES];

        

        [self startAnimation];

        

        [UIView animateWithDuration:0.2 animations:^{

            l.transform = CGAffineTransformIdentity;

        }];

        

    }];

}

– (void)updateTime:(NSTimer *)sender

{

    NSDate *startDate = sender.userInfo[@”start”];

    UILabel *l = sender.userInfo[@”label”];

    l.text = [NSString stringWithFormat:@”%2.2f”, 10.0 + [startDate timeIntervalSinceNow]];

    if ([l.text floatValue] <= 0) {

        [sender invalidate];

        l.text = @”00.00″;

    }

}

– (void)startAnimation

{

    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@”position”];

    pathAnimation.duration = 10.0f; // adjust ??

    pathAnimation.path = self.shape.path;

    

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

    mark.text = @”●”;

    mark.textColor = [UIColor greenColor];

    mark.font = [UIFont boldSystemFontOfSize:40];

    [mark sizeToFit];

    [self.view addSubview:mark];

    [mark.layer addAnimation:pathAnimation forKey:nil];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end