iPhoneケーキカット

ケーキを人数分に切り分けると、おおきさはどのくらいになるかな?というiPhoneアプリを描いてみます。


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

サンプルコード

#import “ViewController.h”

#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]

@interface ViewController ()

@property (strong, nonatomic) CAShapeLayer *cakeShapeLayer;

@property (strong, nonatomic) NSMutableArray *pict;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = UIColorHex(0x4faac7);

    

    [self createCake];

    

    [self addMan:35];

    [self createStepper];

    

}

– (void)createCake

{

    UIView *cake = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)];

    cake.layer.cornerRadius = 150;

    cake.backgroundColor = [UIColor blackColor];

    [self.view addSubview:cake];

    

    self.cakeShapeLayer = [CAShapeLayer layer];

    self.cakeShapeLayer.lineWidth = 2;

    self.cakeShapeLayer.fillColor = [UIColor clearColor].CGColor;

    self.cakeShapeLayer.strokeColor = [UIColor whiteColor].CGColor;

    [cake.layer addSublayer:self.cakeShapeLayer];

}

– (void)createStepper

{

    UIStepper *stepper = [[UIStepper alloc] init];

    stepper.center = CGPointMake(160, 500);

    stepper.stepValue = 1;

    stepper.minimumValue = 1;

    stepper.maximumValue = 6;

    [self.view addSubview:stepper];

    

    [stepper addTarget:self action:@selector(changeStepper:) forControlEvents:UIControlEventValueChanged];

}

– (void)changeStepper:(UIStepper*)sender

{

    if (sender.value > [self.pict count]) {

        float x = 50 * sender.value15;

        [self addMan:x];

    } else {

        [[self.pict lastObject] removeFromSuperview];

        [self.pict removeLastObject];

    }

    

    [self cutCake:[self.pict count]];

}

– (void)addMan:(float)x

{

    UIImageView *pictiv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”man”]];

    pictiv.center = CGPointMake(x, 440);

    [self.view addSubview:pictiv];

    [self.pict addObject:pictiv];

}

– (NSMutableArray*)pict

{

    if (!_pict) {

        _pict = [[NSMutableArray alloc] init];

    }

    return _pict;

}

– (void)cutCake:(int)number

{

    CGPoint o = CGPointMake(150, 150);

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    for (int i=1; i<number; i++) {

        if (i == 1) {

            float x = 150.0 * cos(0) + o.x;

            float y = 150.0 * sin(0) + o.y;

            [path moveToPoint:o];

            [path addLineToPoint:CGPointMake(x, y)];

        }

        

        float angle = i * 2.0 * M_PI / (float)number;

        float x = 150.0 * cos(angle) + o.x;

        float y = 150.0 * sin(angle) + o.y;

        [path moveToPoint:o];

        [path addLineToPoint:CGPointMake(x, y)];

    }

    

    self.cakeShapeLayer.path = path.CGPath;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end