// // MyView.m // ExploringGraphics2 // // Copyright TrailsintheSand.com 2008. All rights reserved. // #import "GraphicsView.h" #import "Point2D.h" #import "Vector2D.h" #import "Object2D.h" @implementation GraphicsView - (id)initWithFrame:(CGRect)frameRect { self = [super initWithFrame:frameRect]; // Create a ball 2D object in the upper left corner of the screen // heading down and right ball = [[Object2D alloc] init]; ball.position = [[Point2D alloc] initWithX:0.0 Y:0.0]; ball.vector = [[Vector2D alloc] initWithX:5.0 Y:4.0]; // Start a timer that will call the tick method of this class // 30 times per second timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/30.0) target:self selector:@selector(tick) userInfo:nil repeats:YES]; return self; } - (void)tick { // Update the balls position [ball move:self.bounds]; // Tell the view that it needs to re-draw itself [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { // Clear the display and draw the ball CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextClearRect(ctx, rect); [ball draw:ctx]; } - (void)dealloc { // Free everything up [timer invalidate]; [ball dealloc]; [super dealloc]; } @end