iPhoneひらがなハタ

この絵はな〜んだ?ぶら下がっている「ひらがな」のはたからえらんでみよう。というかんじで、ひらがなお勉強iPhoneアプリのサンプルコードを描いてみます。


今回使った画像


サンプルを動画でみる

サンプルコード

#import “ViewController.h”

@interface Question : NSObject

@property int no;

@property (readonly) NSString *word;

@property (readonly) NSString *imageName;

-(void)update;

@end

@implementation Question

– (void)update

{

    self.no = arc4random() % 3;

}

– (NSString *)word

{

    NSString *str;

    switch (self.no) {

        case 0: str = @”くるま; break;

        case 1: str = @”ぶた; break;

        case 2: str = @”かめ; break;

        default: break;

    }

    return str;

}

– (NSString *)imageName

{

    NSString *str;

    switch (self.no) {

        case 0: str = @”car”; break;

        case 1: str = @”pig”; break;

        case 2: str = @”tortoise”; break;

        default: break;

    }

    return str;

}

@end

@interface ViewController ()

@property (strong, nonatomic) Question *question;

@property (weak, nonatomic) UIImageView *questBoard;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self color:4];

    

    [self updateQuestion];

    [self updateAnswerBox];

    [self createFlag];

}

– (UIImageView *)questBoard

{

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(50, 200, 220, 160)];

    iv.backgroundColor = [self color:3];

    iv.contentMode = UIViewContentModeScaleAspectFit;

    iv.layer.cornerRadius = 10;

    [self.view addSubview:iv];

    

    self.questBoard = iv;

    return iv;

}

– (void)updateQuestion

{

    if (!self.question) {

        self.question = [[Question alloc] init];

    }

    [self.question update];

    UIImage *img = [UIImage imageNamed:self.question.imageName];

    self.questBoard.image = img;

}

– (void)updateAnswerBox

{

    UIBezierPath *triangle = [UIBezierPath bezierPath];

    [triangle moveToPoint:CGPointMake(0, 0)];

    [triangle addLineToPoint:CGPointMake(50, 0)];

    [triangle addLineToPoint:CGPointMake(25, 70)];

    

    for (int i=0; i<[self.question.word length]; i++) {

        float x = 50 * i + 100;

        float y = 380;

        UIView *ans = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 70)];

        ans.backgroundColor = [self color:1];

        ans.tag = i + 1;

        [self.view addSubview:ans];

        

        CAShapeLayer *sl = [CAShapeLayer layer];

        sl.path = triangle.CGPath;

        sl.fillColor = [UIColor whiteColor].CGColor;

        [ans.layer addSublayer:sl];

    }

}

#define Hiragana @あいうえおかきくけこさしすせそ

– (void)createFlag

{

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 75, 320, 5)];

    line.backgroundColor = [self color:3];

    [self.view addSubview:line];

    

    int flagCount = 5;

    NSMutableArray *word = [[NSMutableArray alloc] init];

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

        if (i < self.question.word.length) {

            [word addObject:[self.question.word substringWithRange:NSMakeRange(i, 1)]];

        } else {

            int n = arc4random() % [Hiragana length];

            [word addObject:[Hiragana substringWithRange:NSMakeRange(n, 1)]];

        }

    }

    [self shuffle:word];

    

    UIBezierPath *triangle = [UIBezierPath bezierPath];

    [triangle moveToPoint:CGPointMake(0, 0)];

    [triangle addLineToPoint:CGPointMake(50, 0)];

    [triangle addLineToPoint:CGPointMake(25, 70)];

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

        float x = 55 * i + 25;

        float y = 80;

        UILabel *flag = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 50, 70)];

        flag.backgroundColor = [self color:1];

        NSMutableAttributedString *mstr = [[NSMutableAttributedString alloc] initWithString:[word objectAtIndex:i]];

        NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];

        paragrapStyle.alignment = NSTextAlignmentCenter;

        paragrapStyle.lineHeightMultiple = 0.5;

        

        [mstr addAttributes:@{

                NSFontAttributeName : [UIFont boldSystemFontOfSize:30],

                NSForegroundColorAttributeName : [self color:0],

                NSStrokeWidthAttributeName : @-1,

                NSStrokeColorAttributeName : [UIColor blackColor],

                NSParagraphStyleAttributeName : paragrapStyle,

                } range:NSMakeRange(0, 1)];

        flag.attributedText = mstr;

        

        [self.view addSubview:flag];

        CAShapeLayer *sl = [CAShapeLayer layer];

        sl.path = triangle.CGPath;

        sl.fillColor = [UIColor blackColor].CGColor;

        flag.layer.mask = sl;

        

        flag.userInteractionEnabled = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

        [flag addGestureRecognizer:tap];

    }

}

– (void)tap:(UITapGestureRecognizer*)sender

{

    UILabel *l = (UILabel*)sender.view;

    NSRange range = [self.question.word rangeOfString:l.text];

    

    if (range.location != NSNotFound) {

        UIView *box = [self.view viewWithTag:range.location + 1];

        box.tag = 0;

        [UIView animateWithDuration:0.5 animations:^{

            l.center = box.center;

        }];

    } else {

        [UIView animateWithDuration:0.5 animations:^{

            l.center = CGPointMake(l.center.x, CGRectGetMaxY(self.view.frame) + 100);

        }];

    }

    

    // clear check

    BOOL clear = YES;

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

        if (v.tag > 0) {

            clear = NO;

        }

    }

    if (clear) {

        [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:1.0];

    }

}

– (void)nextQuestion

{

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

        [v removeFromSuperview];

    }

    

    [self updateQuestion];

    [self updateAnswerBox];

    [self createFlag];

}

– (void)shuffle:(NSMutableArray*)arr

{

    NSUInteger count = [arr count];

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

        int nElements = count – i;

        int n = (arc4random() % nElements) + i;

        [arr exchangeObjectAtIndex:i withObjectAtIndex:n];

    }

}

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:0.8]

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0x35FF5C);

        case 1:

            return UIColorHex(0xB5E837);

        case 2:

            return UIColorHex(0xFFD72F);

        case 3:

            return UIColorHex(0xE89338);

        case 4:

            return UIColorHex(0xFF452C);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end