今日のメニューは「目玉焼き」
飛んでくる卵をタッチで割って、
フライパンの上に落としましょう。
上手く焼けるかな?
というテンションでつくりました。
今日は、おままごと料理ものが作りたかったので。
ポイント
卵の形は、UIBezierPathのaddCurveToPointを二回使って書いてます。
殻の上下は、layerのmaskToBoundsをYESにして、いらない部分を隠す
ことで半分に割れるように見せています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *plate;
NSTimer *timer;
UIView *egg;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
[self createEgg];
[self createPan];
[self startTimer];
}
– (void)createEgg
{
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(40, 0)];
CGPoint cp1, cp2;
egg = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
egg.backgroundColor = [UIColor clearColor];
[self.view addSubview:egg];
// 卵の形
cp1 = CGPointMake(70, 0);
cp2 = CGPointMake(100, 80);
[path addCurveToPoint:CGPointMake(40, 80) controlPoint1:cp1 controlPoint2:cp2];
cp1 = CGPointMake(-20, 80);
cp2 = CGPointMake(10, 0);
[path addCurveToPoint:CGPointMake(40, 0) controlPoint1:cp1 controlPoint2:cp2];
UIView *eggTop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
eggTop.backgroundColor = [UIColor clearColor];
eggTop.layer.masksToBounds = YES;
[egg addSubview:eggTop];
CAShapeLayer *sl = [[CAShapeLayer alloc] initWithLayer:eggTop.layer];
sl.fillColor = [UIColor whiteColor].CGColor;
sl.path = path.CGPath;
[eggTop.layer addSublayer:sl];
UIView *eggBottom = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 80, 30)];
eggBottom.backgroundColor = [UIColor clearColor];
eggBottom.layer.masksToBounds = YES;
[egg addSubview:eggBottom];
CAShapeLayer *slb = [[CAShapeLayer alloc] initWithLayer:eggBottom.layer];
slb.fillColor = [UIColor whiteColor].CGColor;
slb.path = path.CGPath;
slb.position = CGPointMake(0, –50);
[eggBottom.layer addSublayer:slb];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[egg addGestureRecognizer:tap];
}
– (void)createPan
{
plate = [[UIView alloc] initWithFrame:CGRectMake(120, 250, 160, 160)];
plate.layer.cornerRadius = 80;
plate.backgroundColor = [UIColor darkGrayColor];
plate.layer.borderWidth = 2.0;
plate.layer.borderColor = [UIColor blackColor].CGColor;
plate.layer.transform = CATransform3DMakeRotation(M_PI * 0.2, 1, 0, 0);
plate.layer.masksToBounds = YES;
plate.layer.zPosition = – 200;
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
bar.layer.cornerRadius = 10;
bar.backgroundColor = [UIColor blackColor];
bar.center = CGPointMake(280, 400);
bar.transform = CGAffineTransformMakeRotation(M_PI * 0.2);
[self.view addSubview:plate];
[self.view addSubview:bar];
}
– (void)tap:(UITapGestureRecognizer*)gr
{
gr.view.tag = 1;
[UIView animateWithDuration:0.2 animations:^{
gr.view.transform = CGAffineTransformMakeRotation(M_PI*0.5);
} completion:^(BOOL finished) {
for (int i=0; i< [gr.view.subviews count]; i++) {
UIView *shell = [gr.view.subviews objectAtIndex:i];
if (i==0) {
[UIView animateWithDuration:1 animations:^{
CGAffineTransform t = CGAffineTransformMakeTranslation(0, –30);
shell.transform = CGAffineTransformRotate(t, –M_PI * 0.3);
}];
} else {
[UIView animateWithDuration:1 animations:^{
CGAffineTransform t = CGAffineTransformMakeTranslation(0, 30);
shell.transform = CGAffineTransformRotate(t, M_PI * 0.3);
}];
}
}
CGPoint c = CGPointMake(gr.view.center.x –30, gr.view.center.y – 10);
UIView *whites = [[UIView alloc] initWithFrame:CGRectMake(c.x, c.y – 20, 50, 60)];
whites.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
whites.layer.cornerRadius = 25;
[self.view insertSubview:whites belowSubview:gr.view];
UIView *yolk = [[UIView alloc] initWithFrame:CGRectMake(c.x, c.y, 40, 40)];
yolk.layer.cornerRadius = 20;
yolk.backgroundColor = [UIColor yellowColor];
[self.view insertSubview:yolk belowSubview:gr.view];
[UIView animateWithDuration:3 animations:^{
whites.transform = CGAffineTransformMakeScale(1, 2);
whites.center = CGPointMake(yolk.center.x, 320);
yolk.center = CGPointMake(yolk.center.x, 340);
} completion:^(BOOL finished) {
if (CGRectIntersectsRect(whites.frame, plate.frame)) {
yolk.center = [self.view convertPoint:yolk.center toView:plate];
whites.bounds = CGRectMake(0, 0, 80, 80);
whites.transform = CGAffineTransformIdentity;
whites.center = yolk.center;
whites.layer.cornerRadius = 40;
[plate addSubview:whites];
[plate addSubview:yolk];
[UIView animateWithDuration:1.0 animations:^{
whites.backgroundColor = [UIColor whiteColor];
} completion:^(BOOL finished) {
[self restart];
}];
} else {
[self restart];
[UIView animateWithDuration:1 animations:^{
whites.center = CGPointMake(yolk.center.x, 550);
yolk.center = CGPointMake(yolk.center.x, 550);
}];
}
}];
}];
}
– (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:2.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
-(void)tick:(NSTimer*)sender
{
if (egg.tag == 0) {
egg.center = CGPointMake(egg.center.x + 5, egg.center.y);
if (egg.center.x > 320) {
[self restart];
}
}
}
– (void)restart
{
[UIView animateWithDuration:2 animations:^{
egg.alpha = 0;
}];
for (UIView *v in plate.subviews) {
[UIView animateWithDuration:15 animations:^{
v.alpha = 0;
} completion:^(BOOL finished) {
[v removeFromSuperview];
}];
}
[self createEgg];
egg.center = CGPointMake(egg.center.x – 100, egg.center.y);
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end