iPhoneまみむめも

ひらがな「まみむめも」を順番に再生していくiPhoneアプリを描いてみます。


動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (strong, nonatomic) UIView *wordCircel;

@property int count;

@property NSMutableArray *mySounds;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor grayColor];

    

    UIView *mark = [[UIView alloc] initWithFrame:CGRectMake(110, 270, 100, 200)];

    mark.backgroundColor = [UIColor orangeColor];

    mark.layer.cornerRadius = 10;

    [self.view addSubview:mark];

    

    [self createWords];

    [self createPlayButton];

    [self prepareToSounds];

}

– (void)createWords

{

    self.wordCircel = [[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 320)];

    self.wordCircel.backgroundColor = [UIColor clearColor];

    [self.view addSubview:self.wordCircel];

    

    NSArray *words = @[@”,@”,@”,@”,@”];

    float angle = 2.0 * M_PI / 5.0;

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

        NSString *s = [words objectAtIndex:i];

        

        float x = 100 * sin(angle * i) + 160;

        float y = 100 * cos(angle * i) + 160;

        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

        l.center = CGPointMake(x, y);

        l.text = s;

        l.font = [UIFont systemFontOfSize:60];

        l.textColor = [UIColor grayColor];

        l.textAlignment = NSTextAlignmentCenter;

        l.backgroundColor = [UIColor whiteColor];

        l.layer.cornerRadius = 40;

        [self.wordCircel addSubview:l];

    }

}

– (void)createPlayButton

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(30, 20)];

    [path addLineToPoint:CGPointMake(13, 10)];

    [path addLineToPoint:CGPointMake(13, 30)];

    [path closePath];

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.path = path.CGPath;

    sl.fillColor = [UIColor whiteColor].CGColor;

    

    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    playBtn.frame = CGRectMake(140, 400, 40, 40);

    playBtn.backgroundColor = [UIColor colorWithHue:0.5 saturation:1.0 brightness:1.0 alpha:1.0];

    

    CALayer *dark = [CALayer layer];

    dark.frame = CGRectMake(0, 20, 40, 20);

    dark.backgroundColor = [UIColor colorWithHue:0.5 saturation:1.0 brightness:0.7 alpha:1.0].CGColor;

    [playBtn.layer addSublayer:dark];

    [playBtn.layer addSublayer:sl];

    [self.view addSubview:playBtn];

    

    [playBtn addTarget:self action:@selector(playAndReload) forControlEvents:UIControlEventTouchUpInside];

}

– (void)playAndReload

{

    [self play];

    [self performSelector:@selector(reloadWord) withObject:nil afterDelay:0.5];

}

– (void)reloadWord

{

    self.count = (self.count + 1) % 5;

    [UIView animateWithDuration:0.5 animations:^{

        self.wordCircel.transform = CGAffineTransformMakeRotation(self.count * M_PI/2.5);

        [self.wordCircel.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            UIView *v = (UIView*)obj;

            v.transform = CGAffineTransformMakeRotation(-self.count * M_PI/2.5);

        }];

    }];

}

– (void)prepareToSounds

{

    NSArray *names = @[@”ma”,@”mi”,@”mu”,@”me”,@”mo”];

    self.mySounds = [[NSMutableArray alloc] init];

    for (NSString *s in names) {

        NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:s ofType:@”m4a”]];

        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];

        [player prepareToPlay];

        [self.mySounds addObject:player];

    }

}

– (void)play

{

    NSArray *kakiku = @[@”, @”, @”, @”, @”];

    NSUInteger i = [kakiku indexOfObject:[kakiku objectAtIndex:self.count]];

    [(AVAudioPlayer*)self.mySounds[i] play];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end