iPhone輪二つタイマー

遅い輪と速い輪でタイマーを表示するiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface ViewController () <SKSceneDelegate>

@property (nonatomic, weak) SKScene *scene;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createRing];

}

– (void)setupScene {

    SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];

    SKScene *s = [SKScene sceneWithSize:sv.frame.size];

    s.delegate = self;

    s.backgroundColor = [self color:4];

    [sv presentScene:s];

    [self.view addSubview:sv];

    

    self.scene = s;

}

#define r1 CGRectGetMaxX(self.view.bounds) * 0.4

#define r2 CGRectGetMaxX(self.view.bounds) * 0.25

– (void)createRing {

    for (int i=0; i<2; i++) {

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:(float[]){r1, r2}[i] startAngle:0 endAngle:0.1 clockwise:YES];

        SKShapeNode *ring = [SKShapeNode shapeNodeWithPath:path.CGPath];

        ring.name = [NSString stringWithFormat:@”ring%d”, i];

        ring.lineWidth = 20;

        ring.strokeColor = [self color:i + 1];

        ring.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

        [self.scene addChild:ring];

    }

}

– (void)update:(NSTimeInterval)currentTime forScene:(SKScene *)scene

{

    float end0 = (2.0 * M_PI / 100.0) * ((int)(100 * currentTime) % 100);

    float end1 = (2.0 * M_PI / 1000.0) * ((int)(100 * currentTime) % 1000);

    

    for (int i=0; i<2; i++) {

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:(float[]){r1, r2}[i] startAngle:0 endAngle:(float[]){end0, end1}[i] clockwise:YES];

        SKShapeNode *ring = (SKShapeNode *)[self.scene childNodeWithName:[NSString stringWithFormat:@”ring%d”, i]];

        ring.path = path.CGPath;

    }

}

#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000)>>16)/255.0 green:((rgb & 0xFF00)>>8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]

– (UIColor *)color:(int)i {

    if (i > 4) { return nil; }

    int colorCode[] = {0xC194A7, 0xFF92B9, 0xFFC6EB, 0xEFFAFF, 0xCAEE9F};

    return ColorHex(colorCode[i]);

}

@end