ちゃいろい棒をならべて、まわして
まっすぐ、さんかく、しかく
色んな形を作ってみよう。
という感じの子供向けiPhoneゲームのサンプルコード
ポイント
Connectorという名前でUIViewをカスタムしてます。
棒を真ん中の長方形と左右の丸い部分に3分割、
長方形には、PanGestureRecognizer, 左右の丸は、
touchesMovedでくるくる回転を制御しています。
環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface Connector : UIView {
UIView *selectedCorner;
}
@end
@implementation Connector
– (id)initWithFrame:(CGRect)frame
{
CGRect mframe = CGRectMake(frame.origin.x, frame.origin.y, 100, 20);
self = [super initWithFrame:mframe];
if (self) {
[self createBar];
}
return self;
}
– (void)createBar
{
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 80, 20)];
bar.backgroundColor = [UIColor brownColor];
[self addSubview:bar];
UIPanGestureRecognizer *move = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[bar addGestureRecognizer:move];
UIView *circleA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
circleA.layer.cornerRadius = 10;
circleA.backgroundColor = [UIColor brownColor];
circleA.tag = 1;
[self addSubview:circleA];
UIView *circleB = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 20, 20)];
circleB.layer.cornerRadius = 10;
circleB.backgroundColor = [UIColor brownColor];
circleB.tag = 1;
[self addSubview:circleB];
UIView *holeA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 6)];
holeA.backgroundColor = [UIColor blackColor];
holeA.layer.cornerRadius = 3;
holeA.center = CGPointMake(10, 10);
holeA.userInteractionEnabled = NO;
[circleA addSubview:holeA];
UIView *holeB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 6, 6)];
holeB.backgroundColor = [UIColor blackColor];
holeB.layer.cornerRadius = 3;
holeB.center = CGPointMake(10, 10);
holeB.userInteractionEnabled = NO;
[circleB addSubview:holeB];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self];
UIView *selected = [self hitTest:p withEvent:event];
if (selected.tag == 1) {
selectedCorner = selected;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
selectedCorner = nil;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self];
if (selectedCorner) {
[self turn:p];
}
}
– (void)turn:(CGPoint)p
{
CGPoint p1 = p;
CGPoint p0 = [self.superview convertPoint:self.center toView:self];
float angle = atan2f(p1.y – p0.y, p1.x – p0.x);
self.transform = CGAffineTransformRotate(self.transform, angle);
}
– (void)move:(UIPanGestureRecognizer*)gr
{
CGPoint p = [gr locationInView:self.superview];
gr.view.superview.center = p;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createBars];
}
– (void)createBars
{
for (int i=0; i<9; i++) {
float x = (i % 3) * 100 + 10;
float y = (i / 3) * 30 + 320;
Connector *c = [[Connector alloc] initWithFrame:CGRectMake(x, y, 100, 40)];
[self.view addSubview:c];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end