iPhoneフィボナッチ棒

フィボナッチ数列の棒を四角く表示していくiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *fibonacciNumbers;

@property (nonatomic, weak) UIScrollView *scroll;

@property (nonatomic, weak) UIView *contentView;

@property CGPoint lastPoint;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];

    sv.backgroundColor = [UIColor lightGrayColor];

    sv.minimumZoomScale = 0.1;

    sv.delegate = self;

    [self.view addSubview:sv];

    self.scroll = sv;

    UIView *content = [[UIView alloc] initWithFrame:self.view.bounds];

    content.center = self.view.center;

    [sv addSubview:content];

    self.contentView = content;

    [self createZero];

    [self createOne];

    

    [self createButton];

}

– (void)createButton

{

    UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];

    plusButton.frame = CGRectMake(0, 0, 50, 50);

    plusButton.backgroundColor = [UIColor darkGrayColor];

    plusButton.center = CGPointMake(160, CGRectGetMaxY(self.view.bounds) – 50);

    [plusButton setTitle:@”+” forState:UIControlStateNormal];

    [self.view addSubview:plusButton];

    

    [plusButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];

}

– (void)createZero

{

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

    zero.text = @”0″;

    zero.textColor = [UIColor whiteColor];

    [zero sizeToFit];

    zero.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

    [self.contentView addSubview:zero];

    

    self.fibonacciNumbers = [[NSMutableArray alloc] init];

    [self.fibonacciNumbers addObject:@(0)];

}

– (void)createOne

{

    UIView *one = [self createBar:@(1)];

    one.tag = 1;

    one.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) + 10);

    [self.fibonacciNumbers addObject:@(1)];

    

    self.lastPoint = one.center;

}

– (UILabel*)createBar:(NSNumber*)number

{

    float w = MIN(number.longLongValue + 10, 1000); // over flow…

    float h = MIN(self.fibonacciNumbers.count + 10, 50);

    UILabel *bar = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, h)];

    bar.text = [number stringValue];

    bar.font = [UIFont systemFontOfSize:10 + self.fibonacciNumbers.count];

    bar.textColor = [UIColor whiteColor];

    bar.textAlignment = NSTextAlignmentCenter;

    float hue = (self.fibonacciNumbers.count % 10) * 0.1;

    bar.backgroundColor = [UIColor colorWithHue:hue saturation:0.8 brightness:0.9 alpha:0.8];

    [self.contentView addSubview:bar];

    

    return bar;

}

– (void)next

{

    NSNumber *next = [self calcNext];

    int direction = self.fibonacciNumbers.count % 4;

    

    UIView *nextBar = [self createBar:next];

    nextBar.center = self.lastPoint;

    

    float angle = 0.5 * M_PI * (direction + 1);

    float dx = (10 + self.fibonacciNumbers.count*0.1) * self.fibonacciNumbers.count * 0.5 * cos(angle);

    float dy = (10 + self.fibonacciNumbers.count*0.1) * self.fibonacciNumbers.count * 0.5 * sin(angle);

    [UIView animateWithDuration:0.5 animations:^{

        nextBar.center = CGPointMake(nextBar.center.x + dx, nextBar.center.y + dy);

        nextBar.transform = CGAffineTransformMakeRotation(angle + 0.5 * M_PI);

    }];

    

    [self.scroll setZoomScale:1.0self.fibonacciNumbers.count * 0.01 animated:YES];

}

– (NSNumber*)calcNext

{

    NSNumber *fi = [self.fibonacciNumbers lastObject];

    NSNumber *fim1 = [self.fibonacciNumbers objectAtIndex:self.fibonacciNumbers.count2];

    [self.fibonacciNumbers addObject:@([fi intValue]+ [fim1 intValue])];

    

    return [self.fibonacciNumbers lastObject];

}

– (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    self.contentView.center = CGPointMake(160, 160);

    self.contentView.transform = CGAffineTransformMakeRotation(self.fibonacciNumbers.count * M_PI * 0.01);

    return self.contentView;

}

@end