iPhoneリスニングABC

ABCのリスニングするようなiPhoneアプリのサンプルコードを描いてみます。

@import AVFoundation;

#import “ViewController.h”

#define WORDS @“ABCDEFGHIJKLMNOPQRSTUVWXYZ”

@interface ViewController ()

@property (nonatomic, strong) NSMutableDictionary *views;

@property (nonatomic ,strong) NSMutableArray *seeds;

@property (nonatomic, weak) UIView *maker;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.views = [NSMutableDictionary dictionary];

    self.seeds = [NSMutableArray array];

    

    [self createWords];

    [self createListenButton];

    [self createNextButton];

    [self mylayout];

}

– (void)createWords

{

    UIView *wordsView = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 300, 200)];

    wordsView.backgroundColor = [UIColor purpleColor];

    [self.view addSubview:wordsView];

    

    wordsView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.views setObject:wordsView forKey:@”words”];

    

    for (int i=0; i<WORDS.length; i++) {

        NSString *s = [WORDS substringWithRange:NSMakeRange(i, 1)];

        float x = (i % 7) * 35 + 40;

        float y = (i / 7) * 35 + 30;

        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 30, 30)];

        l.textColor = [UIColor whiteColor];

        l.font = [UIFont systemFontOfSize:20];

        l.text = s;

        l.userInteractionEnabled = YES;

        [wordsView addSubview:l];

        

        [self.seeds addObject:s];

    }

    [self shuffle:self.seeds];

    

    

    // marker

    UIView *marker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    marker.layer.cornerRadius = 15;

    marker.backgroundColor = [UIColor orangeColor];

    marker.alpha = 0.4;

    [wordsView addSubview:marker];

    self.maker = marker;

    

    UIBezierPath *path = [UIBezierPath bezierPath];

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

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

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

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

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

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

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

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

    CAShapeLayer *l = [CAShapeLayer layer];

    l.path = path.CGPath;

    l.lineWidth = 3;

    l.fillColor = [UIColor clearColor].CGColor;

    l.strokeColor = [UIColor whiteColor].CGColor;

    [marker.layer addSublayer:l];

}

– (void)createListenButton

{

    UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];

    b.titleLabel.font = [UIFont systemFontOfSize:40];

    [b setTitle:@”Listen” forState:UIControlStateNormal];

    [self.view addSubview:b];

    b.translatesAutoresizingMaskIntoConstraints = NO;

    [self.views setObject:b forKey:@”listen”];

    

    [b addTarget:self action:@selector(listen:) forControlEvents:UIControlEventTouchUpInside];

}

– (void)createNextButton

{

    UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem];

    b.titleLabel.font = [UIFont systemFontOfSize:40];

    [b setTitle:@”next” forState:UIControlStateNormal];

    [self.view addSubview:b];

    

    b.translatesAutoresizingMaskIntoConstraints = NO;

    [self.views setObject:b forKey:@”next”];

    

    [b addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];

}

– (void)mylayout

{

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-10-[words]-10-|” options:0 metrics:nil views:self.views]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-10-[listen]-10-|” options:0 metrics:nil views:self.views]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-10-[next]-10-|” options:0 metrics:nil views:self.views]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-30-[words]-30-[listen(40)]-30-[next(40)]-|” options:0 metrics:nil views:self.views]];

}

– (void)listen:(UIButton *)sender

{

    NSString *s = [self.seeds lastObject];

    AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc]init];

    AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:s.lowercaseString];

    [utterance setRate:AVSpeechUtteranceMinimumSpeechRate];

    [synthesizer speakUtterance:utterance];

}

– (void)next:(UIButton *)sender

{

    [self shuffle:self.seeds];

}

– (void)shuffle:(NSMutableArray *)array

{

    NSUInteger count = [array count];

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

        NSInteger remainingCount = count – i;

        NSInteger exchangeIndex = (arc4random() % remainingCount) + i;

        [array exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];

    }

}

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

{

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

    self.maker.center = p;

}

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

{

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

    self.maker.center = p;

}

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

{

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

    self.maker.userInteractionEnabled = NO;

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

    if ([hit isKindOfClass:[UILabel class]]) {

        if ([self.seeds.lastObject isEqual:((UILabel *)hit).text]) {

            [UIView animateWithDuration:0.3 animations:^{

                hit.transform = CGAffineTransformMakeScale(0.4, 0.4);

            } completion:^(BOOL finished) {

                [self.seeds removeLastObject];

            }];

        }

    }

    self.maker.userInteractionEnabled = YES;

}

@end