9マスの数字を左右にスライドして当てる。といったiPhoneアプリのサンプルコードを描いてみます。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0 green:0.6 blue:0 alpha:1];
[self createSecretBox];
[self createHintBox];
UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeSystem];
checkButton.titleLabel.font = [UIFont boldSystemFontOfSize:50];
[checkButton setTitle:@”check!” forState:UIControlStateNormal];
[checkButton addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchUpInside];
[checkButton sizeToFit];
checkButton.center = CGPointMake(160, 420);
[self.view addSubview:checkButton];
[self updateHints];
}
– (void)createSecretBox
{
for (int i=0; i<9; i++) {
float x = (i % 3) * 60 + 70;
float y = (i / 3) * 60 + 70;
UILabel *box = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 60, 60)];
box.tag = i + 1;
box.textAlignment = NSTextAlignmentCenter;
box.textColor = [UIColor clearColor];
box.text = [@(arc4random() % 10) stringValue];
box.layer.name = @”box”;
box.backgroundColor = (i%2) ? [UIColor colorWithWhite:0.8 alpha:1.0] : [UIColor colorWithWhite:0.2 alpha:1.0];
box.layer.borderColor = [UIColor whiteColor].CGColor;
box.layer.borderWidth = 2;
[self.view addSubview:box];
box.userInteractionEnabled = YES;
UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(right:)];
right.direction = UISwipeGestureRecognizerDirectionRight;
[box addGestureRecognizer:right];
UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(left:)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[box addGestureRecognizer:left];
}
}
– (void)createHintBox
{
for (int i=0; i<5; i++) {
UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(i*60 + 10 , 280, 60, 60)];
number.layer.name = @”hint”;
number.backgroundColor = [UIColor lightGrayColor];
number.textAlignment = NSTextAlignmentCenter;
number.font = [UIFont boldSystemFontOfSize:30];
number.textColor = [UIColor whiteColor];
number.layer.cornerRadius = 20;
number.layer.borderWidth = 5;
number.layer.borderColor = self.view.backgroundColor.CGColor;
[self.view addSubview:number];
}
}
– (void)updateHints
{
NSPredicate *predA = [NSPredicate predicateWithFormat:@”layer.name == %@”, @”hint”];
NSPredicate *predB = [NSPredicate predicateWithFormat:@”layer.name == %@”, @”box”];
NSArray *hints = [self.view.subviews filteredArrayUsingPredicate:predA];
NSArray *numberBox = [self.view.subviews filteredArrayUsingPredicate:predB];
[hints enumerateObjectsUsingBlock:^(UILabel *v, NSUInteger idx, BOOL *stop) {
float x = v.layer.position.x;
NSPredicate *pred = [NSPredicate predicateWithFormat:@”layer.position.x + layer.transform.translation.x == %f”, x];
int sum = [[[numberBox filteredArrayUsingPredicate:pred] valueForKeyPath:@”@sum.text.intValue”] intValue];
v.text = [@(sum) stringValue];
}];
}
– (void)right:(UISwipeGestureRecognizer*)gr
{
float y = gr.view.layer.position.y;
NSPredicate *pred = [NSPredicate predicateWithFormat:@”layer.position.y == %f”, y];
[[self.view.subviews filteredArrayUsingPredicate:pred] enumerateObjectsUsingBlock:^(UIView *v, NSUInteger idx, BOOL *stop) {
CGAffineTransform trans = CGAffineTransformIsIdentity(v.transform) ? CGAffineTransformMakeTranslation(60, 0) : CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^{
v.transform = trans;
}];
}];
[self performSelector:@selector(updateHints) withObject:Nil afterDelay:0.6];
}
– (void)left:(UISwipeGestureRecognizer*)gr
{
float y = gr.view.layer.position.y;
NSPredicate *pred = [NSPredicate predicateWithFormat:@”layer.position.y == %f”, y];
[[self.view.subviews filteredArrayUsingPredicate:pred] enumerateObjectsUsingBlock:^(UIView *v, NSUInteger idx, BOOL *stop) {
CGAffineTransform trans = CGAffineTransformIsIdentity(v.transform) ? CGAffineTransformMakeTranslation(-60, 0) : CGAffineTransformIdentity;
[UIView animateWithDuration:0.5 animations:^{
v.transform = trans;
}];
}];
[self performSelector:@selector(updateHints) withObject:Nil afterDelay:0.6];
}
– (void)check:(UIButton*)sender
{
NSPredicate *predB = [NSPredicate predicateWithFormat:@”layer.name == %@”, @”box”];
NSArray *numberBox = [self.view.subviews filteredArrayUsingPredicate:predB];
if ([sender.titleLabel.text isEqual:@”check!”]) {
//show answer
[sender setTitle:@”start” forState:UIControlStateNormal];
[numberBox enumerateObjectsUsingBlock:^(UILabel *l, NSUInteger idx, BOOL *stop) {
l.textColor = [UIColor whiteColor];
}];
} else {
// restart
[sender setTitle:@”check!” forState:UIControlStateNormal];
[numberBox enumerateObjectsUsingBlock:^(UILabel *l, NSUInteger idx, BOOL *stop) {
l.text = [@(arc4random() % 10) stringValue];
l.textColor = [UIColor clearColor];
}];
[self updateHints];
}
}
@end