ABCの文字を大きい円、中くらいの円、小さい円に切り分けて、クルクル位置をあわせるパズルのiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic) float startAngle;
@property (nonatomic) CGPoint startPoint;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self color:2];
[self createOuterPiece];
[self createMidPiece];
[self createCenterPiece];
}
– (void)createOuterPiece
{
UILabel *label = [[UILabel alloc] init];
label.text = @”ABC”;
label.font = [UIFont boldSystemFontOfSize:150];
label.textColor = [self color:3];
[label sizeToFit];
label.center = CGPointMake(150, 150);
UIView *piece = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
piece.center = CGPointMake(160, 250);
piece.layer.cornerRadius = 150;
piece.layer.masksToBounds = YES;
[piece addSubview:label];
[self.view addSubview:piece];
piece.backgroundColor = [self color:0];
UIPanGestureRecognizer *turn = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(turn:)];
[piece addGestureRecognizer:turn];
}
– (void)createMidPiece
{
UILabel *label = [[UILabel alloc] init];
label.text = @”ABC”;
label.font = [UIFont boldSystemFontOfSize:150];
label.textColor = [self color:3];
[label sizeToFit];
label.center = CGPointMake(100, 100);
UIView *piece = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
piece.center = CGPointMake(160, 250);
piece.layer.cornerRadius = 100;
piece.layer.masksToBounds = YES;
[piece addSubview:label];
[self.view addSubview:piece];
piece.backgroundColor = [self color:1];
UIPanGestureRecognizer *turn = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(turn:)];
[piece addGestureRecognizer:turn];
}
– (void)createCenterPiece
{
UILabel *a = [[UILabel alloc] init];
a.text = @”ABC”;
a.font = [UIFont boldSystemFontOfSize:150];
a.textColor = [self color:3];
[a sizeToFit];
a.center = CGPointMake(50, 50);
UIView *piece = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
piece.center = CGPointMake(160, 250);
piece.layer.cornerRadius = 50;
piece.layer.masksToBounds = YES;
[piece addSubview:a];
[self.view addSubview:piece];
piece.backgroundColor = [self color:2];
UIPanGestureRecognizer *turn = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(turn:)];
[piece addGestureRecognizer:turn];
}
– (void)turn:(UIPanGestureRecognizer*)gr
{
if (gr.state == UIGestureRecognizerStateBegan) {
self.startAngle = [[gr.view.layer valueForKeyPath:@”transform.rotation.z”] floatValue];
self.startPoint = [gr locationInView:self.view];
}
CGPoint p1 = [gr locationInView:self.view];
CGPoint p0 = gr.view.center;
float angle = atan2f(p1.y – p0.y, p1.x – p0.x);
float angle0 = atan2f(self.startPoint.y – p0.y, self.startPoint.x – p0.x);
gr.view.transform = CGAffineTransformMakeRotation(angle – angle0 + self.startAngle);
}
#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:1.0]
– (UIColor*)color:(int)i
{
switch (i) {
case 0:
return UIColorHex(0x4db506);
case 1:
return UIColorHex(0x259dea);
case 2:
return UIColorHex(0xff9712);
case 3:
return UIColorHex(0xc30006);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end