英語のフレーズを正しい順番に並べて遊ぶアプリの落書き

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

操作

・フレーズの入力

・単語をランダムにばらす

・単語カードを上にスワイプすることで回答

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () <UITextViewDelegate> {

    int hit;

    CGPoint goalPosition;

}

@property (nonatomic, strong) UIView *goal;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) NSArray *words;

@end

@implementation ViewController

@synthesize goal, label;

– (void)viewDidLoad

{

    [super viewDidLoad];

}

– (void)viewDidAppear:(BOOL)animated

{

    // タイトルを表示

    [self showTitle];

    

    // 入力欄を作成

    [self createInputField];

}

– (void)showTitle

{

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 0, 0)];

    title.text = @”Learn a English phrase”;

    title.font = [self thisAppFontStyle:0];

    [title sizeToFit];

    [self.view addSubview:title];

}

– (void)createInputField

{

    UITextView *field = [[UITextView alloc] initWithFrame:CGRectMake(10, 70, 300, 100)];

    field.returnKeyType = UIReturnKeyDone;

    field.delegate = self;

    field.layer.cornerRadius = 5.0;

    field.layer.borderColor = [UIColor greenColor].CGColor;

    field.layer.borderWidth = 3.0;

    // margin

    field.contentInset = UIEdgeInsetsMake(10,10,0,0);

    field.font = [self thisAppFontStyle:1];

    [self.view addSubview:field];

}

// フォントを2種類用意しておく

– (UIFont*)thisAppFontStyle:(int)type

{

    if (type == 0) {

        return [UIFont fontWithName:@”Chalkduster” size:20];

    } else {

        return [UIFont fontWithName:@”Noteworthy-Light” size:15];

    }

}

– (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    // Return が押されたら、キーボードを隠し、入力を終了する

    if([text isEqualToString:@”\n”]) {

        [textView resignFirstResponder];

        [self createBox];

        return NO;

    }

    return YES;

}

– (BOOL)textViewShouldEndEditing:(UITextView *)textView

{

    // 入力が終わったら、パネルを作成

    [self createPanels:textView.text];

    

    

    // パネルをばらばらにして下に並べる。

    [self shufflePosition];

    

    return YES;

}

– (void)createPanels:(NSString*)phrase

{

    self.words = [phrase componentsSeparatedByString:@” “];

    for (int i=0; i<[self.words count]; i++) {

        int x = i % 3;

        int y = i / 3;

        UILabel *word = [[UILabel alloc] initWithFrame:CGRectMake(x * 100 + 20, y * 60 + 250, 0, 0)];

        word.text = [NSString stringWithFormat:@”%@”, [self.words objectAtIndex:i]];

        word.font = [self thisAppFontStyle:1];

        [word sizeToFit];

        // add margin

        word.bounds = CGRectMake(0,0, word.bounds.size.width+10, word.bounds.size.height+10);

        word.textAlignment = 1; // center

        word.backgroundColor = [UIColor lightGrayColor];

        word.layer.shadowColor = [UIColor blackColor].CGColor;

        word.layer.shadowOffset = CGSizeMake(5, 5);

        word.layer.shadowOpacity = 1.0;

        word.layer.masksToBounds = NO;

        word.layer.zPosition = 10;

        word.tag = 1;

        [self.view addSubview:word];

        

        word.userInteractionEnabled = YES;

        UISwipeGestureRecognizer *gr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(throwPanel:)];

        gr.direction = UISwipeGestureRecognizerDirectionUp;

        [word addGestureRecognizer:gr];

    }

}

– (void)shufflePosition

{

    // パネルを一時的に配列に退避

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

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

        if (v.tag == 1) { // panel tag = 1に設定

            [wordsLabel addObject:v];

        }

    }

    

    

    NSMutableArray *buf = [wordsLabel mutableCopy];

    // 座標をシャッフルする

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

    int size = [buf count];

    int counter = 0;

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

        int random = arc4random() % (size – counter);

        UILabel *l = [buf objectAtIndex:random];

        [shuffle addObject:[NSValue valueWithCGPoint:l.center]];

        [buf removeObjectAtIndex:random];

        counter++;

    }

    

    // 配置を換える

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

        UIView *v = [wordsLabel objectAtIndex:i];

        v.center = [[shuffle objectAtIndex:i] CGPointValue];

    }

}

– (void)throwPanel:(UISwipeGestureRecognizer*)gr

{

    // 投げた文字

    UILabel *word = (UILabel*)gr.view;

    

    // 間違ってたら、戻すために元の場所を保持

    CGPoint old = word.center;

    

    // 正否判定

    BOOL correct = [word.text isEqual:[self.words objectAtIndex:hit]];

    

    

    [UIView animateWithDuration:0.5 animations:^{

        word.center = self.goal.center;

    } completion:^(BOOL finished) {

        if (correct) {

            hit++;

            label.text = [NSString stringWithFormat:@”hit: %d / total: %d”, hit, [self.words count]];

            goalPosition.x += word.bounds.size.width + 10;

            word.center = [self.goal convertPoint:goalPosition toView:self.view];

            

            if (hit == [self.words count]) {

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

                message.text = @”Clear”;

                message.font = [self thisAppFontStyle:0];

                message.textColor = [UIColor blueColor];

                [message sizeToFit];

                message.center = CGPointMake(self.view.center.x, 600);

                [self.view addSubview:message];

                [UIView animateWithDuration:1 animations:^{

                    message.center = self.view.center;

                }];

            }

            

        } else {

            [UIView animateWithDuration:0.5 animations:^{

                word.center = old;

            }];

        }

    }];

}

– (void)createBox

{

    goal = [[UIView alloc] initWithFrame:CGRectMake(10, 70, 300, 100)];

    goal.layer.cornerRadius = 30.0;

    goal.backgroundColor = [UIColor redColor];

    [self.view addSubview:goal];

    label = [[UILabel alloc] init];

    label.text = [NSString stringWithFormat:@”hit: %d / total: %d”, hit, [self.words count]];

    label.font = [self thisAppFontStyle:0];

    label.textColor = [UIColor whiteColor];

    [label sizeToFit];

    label.backgroundColor = [UIColor clearColor];

    label.center = CGPointMake(150, 80);

    [goal addSubview:label];

    

    

    goalPosition = CGPointMake(10, 10);

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end