Soundstep
Soundstep

Reputation: 618

Best pratice to draw or make thing moving in a UIView

New to Objective-C for iPhone, I'm making some experiment.

In a case where I make 2 squares moving on the screen, I've tried two methods where it doesn't seem to make any difference. But I guess it might be with heavier graphics.

Both Methods are updated on a timer:

[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(update) userInfo:nil repeats:YES];

1- moving

The first method I tried is moving the center point of the square UIView in an update method:

mySquare.center = CGPointMake(mySquare.center.x+0.5, mySquare.center.y+0.5);

2- drawing

The second method is changing the frame property of the square UIView and redraw the view in an update method:

[mySquare update:CGRectMake(mySquare.frame.origin.x+0.5, mySquare.frame.origin.y+0.5, mySquare.frame.size.width, mySquare.frame.size.height)];

In the Square UIView update method:

[self setFrame:rect];
[self setNeedsDisplay];

So, between moving and drawing.

What would be the best to make things moving with Objective-C? What will give the best performance? What would be the best practice?

Thanks

Romu

Upvotes: 0

Views: 267

Answers (1)

Kristopher Johnson
Kristopher Johnson

Reputation: 82545

The right way to do this is to use Core Animation. Take a look at the MoveMe sample application.

Upvotes: 1

Related Questions