昔の駄菓子屋とかこんな感じの電球の動くルーレット

(XcodeのiOS6 Simulatorで試しています。)

ポイント

・マスは、円弧のUIViewを作って貼付け

・ライトはUIView.tagを管理してタイマーで点滅させる

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

// ルーレットのマスにつかう円弧用にUIView

@interface ArcView : UIView

@property (nonatomic, assign) float startAngle;

@property (nonatomic, assign) float endAngle;

@property (nonatomic, strong) UIColor *fillColor;

@end

@implementation ArcView

@synthesize startAngle, endAngle, fillColor;

– (void)drawRect:(CGRect)rect

{

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    [fillColor setFill];

    

    CGPoint origin = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);

    CGContextMoveToPoint(context, origin.x, origin.y);

    CGContextAddArc(context, origin.x, origin.y, origin.x, startAngle, endAngle, NO);

    CGContextDrawPath(context, kCGPathFill);

}

@end

// ViewController

@interface ViewController() {

    NSTimer *timer;

    int lightPosition;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor colorWithRed:0 green:0.8 blue:0 alpha:1.0];

    [self createRouletteBoard];

    

    [self start];

}

// ルーレットの数字

const int numbers[] = {6, 21, 33, 16, 4, 23, 35, 14, 2 ,0, 28, 9, 26, 30, 11, 7, 20, 32, 17, 5, 22, 34, 15, 3, 24, 36, 13, 1, 00, 27, 10, 25, 29, 12, 8, 19, 31, 18, };

– (void)createRouletteBoard

{

    CGPoint origin = CGPointMake(160, 160);

    

    UIView *board = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

    board.layer.cornerRadius = 150;

    board.backgroundColor = [UIColor blackColor];

    board.center = origin;

    [self.view addSubview:board];

  

    int count = (sizeof(numbers) / sizeof(int));

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

        // 円弧

        ArcView *arc = [[ArcView alloc] initWithFrame:board.frame];

        arc.backgroundColor = [UIColor clearColor];

        arc.fillColor = i % 2 ? [UIColor redColor] : [UIColor blackColor];

        float dAngle = 2* M_PI / count;

        arc.startAngle = dAngle * i;

        arc.endAngle = dAngle * (i+1);

        [self.view addSubview:arc];

        

        // ラベル

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

        numberLabel.text = [NSString stringWithFormat:@”%d”, numbers[i]];

        numberLabel.font = [UIFont boldSystemFontOfSize:14];

        numberLabel.textColor = [UIColor whiteColor];

        numberLabel.backgroundColor = [UIColor clearColor];

        [numberLabel sizeToFit];

        float th = dAngle * (i + 0.5);

        numberLabel.transform = CGAffineTransformMakeRotation(th – M_PI * 0.5);

        numberLabel.center = CGPointMake(160 + 140 * cos(th), 160 + 140 * sin(th));

        [self.view addSubview:numberLabel];

        

        //電球

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

        bulb.backgroundColor = [UIColor colorWithRed:0.3 green:0 blue:0 alpha:1];

        bulb.center = CGPointMake(160 + 120 * cos(th), 160 + 120 * sin(th));

        bulb.layer.cornerRadius = 5;

        bulb.tag = i + 1;

        [self.view addSubview:bulb];

    }

    

    UIView *inner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 220)];

    inner.layer.cornerRadius = 110;

    inner.center = origin;

    inner.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:inner];

}

– (void)start

{

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

}

// カウンター

static int speedCounter;

– (void)tick:(NSTimer*)sender

{

    // 電球の光を動かす

    if (speedCounter > 0) {

        [self moveLight];

        speedCounter–;

    }

}

– (void)moveLight

{

    lightPosition = lightPosition + 1;

    if ( lightPosition > sizeof(numbers) / sizeof(int) )

    {

        lightPosition = 1;

    }

    for (UIView *v in self.view.subviews) {

        if (v.tag > 0) {

            if (v.tag == lightPosition) {

                v.backgroundColor = [UIColor yellowColor];

            } else {

                v.backgroundColor = [UIColor colorWithRed:0.3 green:0 blue:0 alpha:1];

            }

        }

    }

}

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

{

    speedCounter = arc4random() % 200 + 100;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end