野菜を切るような、おままごと
(XcodeのiOS6 iPhone Simulatorで動かしています。)
ポイント
野菜のViewの上を指が通ったときにtouchイベントが発生するように
親のViewのtouchイベントを実装する。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
typedef enum {
Carotto,
WhiteRadish,
Potato
} Name;
@interface Vegetable : UIView {
BOOL startPoint;
CGMutablePathRef cutline;
Name name;
}
@end
@implementation Vegetable
– (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
startPoint = YES;
name = arc4random() % 3;
}
return self;
}
– (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setStroke];
CGSize size = self.bounds.size;
if (name == Carotto) {
[[UIColor orangeColor] setFill];
CGContextFillEllipseInRect(ctx, CGRectMake(0.25 * size.width, 0, size.width, 0.3 * size.height));
} else if (name == WhiteRadish) {
[[UIColor whiteColor] setFill];
CGContextFillEllipseInRect(ctx, CGRectMake(0.1 * size.width, 0, size.width, 0.5 * size.height));
} else {
[[UIColor brownColor] setFill];
CGContextFillEllipseInRect(ctx, CGRectMake(0, 0.25 * size.width, 0.5 * size.width, 0.5 * size.height));
}
[[UIColor clearColor] setStroke];
CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextAddPath(ctx, cutline);
CGContextSetLineWidth(ctx, 4.0);
CGContextStrokePath(ctx);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint point = [[touches anyObject] locationInView:self];
// 切り口だったらこっち
if (startPoint) {
startPoint = NO;
if (!cutline) {
cutline = CGPathCreateMutable();
}
CGPathMoveToPoint(cutline, nil, point.x, point.y);
} else {
// 途中の場合
CGPathAddLineToPoint(cutline, nil, point.x, point.y);
[self setNeedsDisplay];
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
startPoint = YES;
}
– (void)dealloc
{
CGPathRelease(cutline);
}
@end
@interface ViewController () {
NSTimer *timer;
NSMutableArray *vegetables;
AVAudioPlayer *player;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createBackGround];
[self createVegetable];
[self start];
}
– (void)createBackGround
{
for (int i=0; i<100; i++) {
int x = i / 10;
int y = i % 10;
UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(x*32, y*50, 32, 50)];
panel.backgroundColor = (x+y) % 2 ? [UIColor lightGrayColor] : [UIColor grayColor];
[self.view addSubview:panel];
}
}
– (void)createVegetable
{
float y = (arc4random() % 6 + 2) * 50;
Vegetable *vege = [[Vegetable alloc] initWithFrame:CGRectMake(330, y, 100, 100)];
[self.view addSubview:vege];
if (!vegetables) {
vegetables = [[NSMutableArray alloc] init];
}
[vegetables addObject:vege];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIView *v in vegetables) {
CGPoint p = [[touches anyObject] locationInView:self.view];
if (v.tag == 0 && CGRectContainsPoint(v.frame, p)) {
v.tag = 1;
[self playSound];
}
[v touchesMoved:touches withEvent:event];
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIView *v in vegetables) {
if (v.tag == 1) {
v.tag = 0;
[v touchesEnded:touches withEvent:event];
}
}
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:2.0/60.0 target:self selector:@selector(updateDisp:) userInfo:nil repeats:YES];
}
– (void)updateDisp:(NSTimer *)sender
{
static int counter;
counter++;
if (counter > 90) {
[self createVegetable];
counter = 0;
}
// 野菜を右から左へ
for (UIView *v in vegetables) {
float x = v.center.x – 1.0;
float y = v.center.y;
v.center = CGPointMake(x, y);
if (x < –100) {
[v removeFromSuperview];
}
}
}
– (void)playSound
{
dispatch_queue_t downloadQueue = dispatch_queue_create(“audio”, NULL);
dispatch_async(downloadQueue, ^{
NSString *path = [[NSBundle mainBundle] pathForResource:@”cut” ofType:@”mp3″];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
player.currentTime = 0;
[player play];
});
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end