福引きのガラガラとかビンゴみたいなもの。

あらかじめ用意しておく玉をランダムに選び出すといった

用途で利用できるかもしれないのでメモ。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *box;

@property (nonatomic, strong) UIView *garagara;

@end

@implementation ViewController

@synthesize box, garagara;

// 数字の配列をセットアップ

– (void)setup

{

    box = [[NSMutableArray alloc] init];

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

        [box addObject:[NSNumber numberWithInt:i]];

    }

}

// 配列からランダムに数字を取り出す

– (int)randomNumber

{

    if ([box count] > 0) {

        int index = arc4random() % [box count];

        NSNumber *choice = [box objectAtIndex:index];

        [box removeObject:choice];

        return [choice intValue];

    }

    return 0;

}

// ここから下は、ガラガラを使ったサンプルの実装

– (void)viewDidLoad

{

    [super viewDidLoad];

    // ガラガラの玉をセットアップ

    [self setup];

    

    

    // 支え

    UIView *frame = [[UIView alloc] initWithFrame:CGRectMake(self.view.center.x65, self.view.center.y100, 30, 70)];

    frame.backgroundColor = [UIColor brownColor];

    frame.layer.cornerRadius = 5.0;

    frame.layer.borderColor = [UIColor blackColor].CGColor;

    frame.layer.borderWidth = 5.0;

    [self.view addSubview:frame];

    

    // 本体

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

    garagara.center = CGPointMake(self.view.center.x50, self.view.center.y100);

    garagara.backgroundColor = [UIColor redColor];

    garagara.layer.cornerRadius = 50.0;

    [self.view addSubview:garagara];

    

    // ハンドル

    UIView *handle = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 10, 10)];

    handle.layer.cornerRadius = 5.0;

    handle.backgroundColor = [UIColor brownColor];

    [garagara addSubview:handle];

    

}

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

{    

    // 回転

    [self turnAnimation:self.garagara];

    

    //

    UIView *ball = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];

    ball.layer.cornerRadius = 12.0;

    ball.backgroundColor = [UIColor blackColor];

    ball.center = self.view.center;

    [self.view addSubview:ball];

    

    int val = [self randomNumber];

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

    label.textColor = [UIColor whiteColor];

    label.text = [NSString stringWithFormat:@”%d”, val];

    label.backgroundColor = [UIColor clearColor];

    [label sizeToFit];

    label.center = CGPointMake(ball.frame.size.width / 2.0, ball.frame.size.height / 2.0);

    [ball addSubview:label];

    

    

    [UIView animateWithDuration:1 animations:^{

        ball.center = CGPointMake(30 * ([box count] % 10) + 20, 350 + 50 * ([box count] / 10) );

    }];

}

– (void)turnAnimation:(UIView*)view

{

    // アニメーション

    double rotation = M_PI * 0.2;

    int repeat = 10;

    double duration = 0.1;

    

    CABasicAnimation* animation;

    animation = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];

    animation.toValue = [NSNumber numberWithFloat: rotation];

    animation.duration = duration;

    animation.repeatCount = repeat;

    animation.cumulative = YES;

    animation.fillMode = kCAFillModeForwards;

    animation.removedOnCompletion = NO;

    [view.layer addAnimation:animation forKey:@”rotationAnimation”];

}

@end