子供の髪型が今流行のおかっぱカットです。
前髪をパッツンに揃えるときのあの楽しさを
iPhoneゲームで再現してみようとサンプルを書いてみました。
これなら遠慮なく前髪をはさみでバッサリいけます。

ポイント
おかっぱと言えば、黒髪とおもったのですが、
なんとなく、真ん中のキャラクターだけ金髪にしました。
前髪は他の髪の毛とは違うUIViewで生成しておいて、
はさみのframeの一部が触れたらanimationで下に
落とすようにしています。
はさみのちょきちょきは、CABasicAnimationです。

環境
ここに書いてあるiPhoneゲームのサンプルコードは、
XcodeのiOS6 iPhone Simulatorで動かしています。


サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *scissors;

    NSMutableArray *bangs;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithHue:0.3 saturation:1.0 brightness:0.5 alpha:1.0];

    

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

        UIColor *hairColor = [UIColor blackColor];

        if (i == 4) {

            //204, 153, 0

            hairColor = [UIColor colorWithRed:204.0/255.0 green:153.0/255.0 blue:0 alpha:1.0];

        }

        UIView *kid = [self createKid:hairColor];

        kid.center = CGPointMake((i/3) * 100 + 60, (i%3) * 150 + 50);

    }

}

– (UIView*)createKid:(UIColor*)hairColor

{

    UIView *kid = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    kid.backgroundColor = [UIColor clearColor];

    [self.view addSubview:kid];

    

    // hear

    UIBezierPath *path = [[UIBezierPath alloc] init];

    [path moveToPoint:CGPointMake(0, 80)];

    CGPoint cp1 = CGPointMake(0, 0);

    CGPoint cp2 = CGPointMake(100, 0);

    [path addCurveToPoint:CGPointMake(100, 80) controlPoint1:cp1 controlPoint2:cp2];

    cp1 = CGPointMake(100, 100);

    cp2 = CGPointMake(0, 100);

    [path addCurveToPoint:CGPointMake(0, 80) controlPoint1:cp1 controlPoint2:cp2];

    CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:kid.layer];

    sl.strokeColor = hairColor.CGColor;

    sl.fillColor = hairColor.CGColor;

    sl.lineWidth = 2.0;

    sl.path = path.CGPath;

    [kid.layer addSublayer:sl];

    

    // face

    UIView *face = [[UIView alloc] initWithFrame:CGRectMake(25, 70, 50, 30)];

    face.layer.cornerRadius = 10;

    // 255 220 178

    face.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:220.0/255.0 blue:178.0/255.0 alpha:1.0];

    [kid addSubview:face];

    

    CALayer *eyeR = [[CALayer alloc] init];

    eyeR.frame = CGRectMake(40, 10, 5, 5);

    eyeR.backgroundColor = [UIColor blackColor].CGColor;

    eyeR.cornerRadius = 2.5;

    

    CALayer *eyeL = [[CALayer alloc] init];

    eyeL.frame = CGRectMake(5, 10, 5, 5);

    eyeL.backgroundColor = [UIColor blackColor].CGColor;

    eyeL.cornerRadius = 2.5;

    

    CALayer *mouth = [[CALayer alloc] init];

    mouth.frame = CGRectMake(15, 20, 20, 5);

    mouth.backgroundColor = [UIColor blackColor].CGColor;

    mouth.cornerRadius = 2.5;

    

    [face.layer addSublayer:eyeR];

    [face.layer addSublayer:eyeL];

    [face.layer addSublayer:mouth];

    

    if (!bangs) {

        bangs = [[NSMutableArray alloc] init];

    }

    

    // bangs

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

        float x = i + 25;

        float y = 70;

        UIView *b = [[UIView alloc] initWithFrame:CGRectMake(x, y, 1, 20)];

        b.backgroundColor = hairColor;

        b.tag = i + 10;

        [kid addSubview:b];

        [bangs addObject:b];

    }

    

    return kid;

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    if (!scissors) {

        [self createScissors];

    }

    CGPoint p = [[touches anyObject] locationInView:self.view];

    scissors.center = CGPointMake(p.x, p.y50);

}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    

    scissors.center = CGPointMake(p.x, p.y50);

    CGRect cutBlade = CGRectMake(p.x20, p.y50, 20, 2);

        

    for (UIView *b in bangs) {

        if (b.center.x < 29 || b.center.x > 71) {

            continue;

        }

        CGRect globalRect = [b.superview convertRect:b.frame toView:self.view];

        globalRect.size = CGSizeMake(globalRect.size.width+=3, globalRect.size.height);

        if (CGRectIntersectsRect(globalRect, cutBlade)) {

            [UIView animateWithDuration:0.5 animations:^{

                b.center = CGPointMake(b.center.x, b.center.y + 50);

                b.alpha = 0.5;

            } completion:^(BOOL finished) {

                [b removeFromSuperview];

            }];

        }

    }

}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    [scissors removeFromSuperview];

    scissors = nil;

}

– (void)createScissors

{

    UIColor *sColor = [UIColor lightGrayColor];

    

    scissors = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    scissors.backgroundColor = [UIColor clearColor];

    

    UIView *a = [[UIView alloc] initWithFrame:CGRectMake(0, 23, 50, 4)];

    a.backgroundColor = sColor;

    a.transform = CGAffineTransformMakeRotation(M_PI * 0.1);

    UIView *aGrip = [[UIView alloc] initWithFrame:CGRectMake(32, 0, 20, 15)];

    aGrip.layer.cornerRadius = 10;

    aGrip.layer.borderWidth = 4;

    aGrip.layer.borderColor = sColor.CGColor;

    [a addSubview:aGrip];

    [scissors addSubview:a];

        

    UIView *b = [[UIView alloc] initWithFrame:CGRectMake(0, 23, 50, 4)];

    b.backgroundColor = sColor;

    b.transform = CGAffineTransformMakeRotation(-M_PI * 0.1);

    UIView *bGrip = [[UIView alloc] initWithFrame:CGRectMake(32, 4, 20, –15)];

    bGrip.layer.cornerRadius = 10;

    bGrip.layer.borderWidth = 4;

    bGrip.layer.borderColor = sColor.CGColor;

    [b addSubview:bGrip];

    [scissors addSubview:b];

    [self.view addSubview:scissors];

    

    NSArray *blades = [NSArray arrayWithObjects:a,b, nil];

    for (UIView *blade in blades) {

        CABasicAnimation *cut = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];

        cut.fromValue = [blade.layer valueForKeyPath:@”transform.rotation.z”];

        cut.toValue = [NSNumber numberWithFloat:M_PI * 0.01];

        cut.repeatCount = 100;

        cut.duration = 0.2;

        cut.autoreverses = YES;

        [blade.layer addAnimation:cut forKey:@”cut”];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end