iPhone唐草模様

螺旋のようなパーツをつかって唐草模様をつくっていくiPhoneアプリを書いてみます。


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor greenColor];

    

    UIView *hole = [[UIView alloc] initWithFrame:CGRectMake(-100, –100, 200, 190)];

    hole.backgroundColor = [UIColor blackColor];

    hole.layer.cornerRadius = 100;

    [self.view addSubview:hole];

    

    [self createGoldenSpiral];

}

– (void)createGoldenSpiral

{

    float r = 50;

    CGPoint o = CGPointMake(55, 5);

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    float startAngle = M_PI;

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

        float endAngle = startAngle – (M_PI/2.0);

        [path addArcWithCenter:o radius:r startAngle:startAngle endAngle:endAngle clockwise:NO];

        

        float d = r * (1.01.0/1.618);

        o = CGPointMake(o.x + d*cos(endAngle), o.y + d * sin(endAngle));

        

        r = r / 1.618;

        startAngle = endAngle;

    }

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.strokeColor = [UIColor whiteColor].CGColor;

    sl.path = path.CGPath;

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.lineWidth = 5;

    

    UIView *spiral = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 60)];

    spiral.backgroundColor = [UIColor clearColor];

    [spiral.layer addSublayer:sl];

    [self.view addSubview:spiral];

    

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];

    [spiral addGestureRecognizer:pan];

    

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(press:)];

    [spiral addGestureRecognizer:longPress];

    

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

    [spiral addGestureRecognizer:tap];

}

– (void)move:(UIPanGestureRecognizer*)gr

{

    if (gr.state == UIGestureRecognizerStateBegan) {

        CGPoint p = [gr locationInView:gr.view];

        gr.view.layer.anchorPoint = CGPointMake(p.x / gr.view.frame.size.width, p.y / gr.view.frame.size.height);

    }

    

    CGPoint p = [gr locationInView:self.view];

    gr.view.layer.position = p;

    

    if (gr.state == UIGestureRecognizerStateEnded) {

        if(gr.view.tag == 0) {

            gr.view.tag = 1;

            [self createGoldenSpiral];

        }

    }

}

– (void)press:(UILongPressGestureRecognizer*)gr

{

    static NSTimer *timer;

    if (gr.state == UIGestureRecognizerStateBegan) {

        CGPoint p = [gr locationInView:gr.view];

        gr.view.layer.anchorPoint = CGPointMake(p.x / gr.view.frame.size.width, p.y / gr.view.frame.size.height);

        gr.view.layer.position = [gr locationInView:self.view];

        timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(turn:) userInfo:gr.view repeats:YES];

    } else {

        [timer invalidate];

        timer = nil;

    }

}

– (void)flip:(UITapGestureRecognizer*)gr

{

    gr.view.layer.transform = CATransform3DRotate(gr.view.layer.transform, M_PI, 0, 1, 0);

}

– (void)turn:(NSTimer *)sender

{

    UIView *v = sender.userInfo;

    v.layer.transform = CATransform3DRotate(v.layer.transform, M_PI * 0.01, 0, 0, 1);

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end