views:

95

answers:

2

Hi everyone,

I have this big problem that i dont know how to fix. I have a UIView that i want to draw a scrolling background on. I am using NSTimer to update 30 frames per second but it seems to redraw one frame every 8 seconds. I am calling [self setNeedsDisplay] but it has no effect. I cant figure out why this is happening, does anyone have any tips?

Thanks for your time.

I have an NSTimer in my applicationDidFinishLaunching method

timer = [NSTimer scheduledTimerWithTimeInterval:0.0
        target:self
        selector:@selector(gameLoop:)
        userInfo:nil
        repeats:YES];

It's messaging this method:

-(void) gameLoop: (id) sender
{
    [myView updateAll];
    [myView setNeedsDisplay];
}

myView is a UIView. UpdateAll updates my code for drawing. It works fine, the problem seems to be the drawRect method is being executed so infrequently. I need to know how to make it execute 30 frames per second. I keep seeing people say that all you have to do is what i have above but setNeedsDisplay still only gets called every 8 seconds.

Thank you for your help...

EDIT

O.K it turns out that my code was just incredibly inefficient and that was causing the slow down not the timer. If you have a similar problem make sure to test your drawRect with some simple drawing (such as drawing your Frames Per Second to the screen) before you assume it is not being executed. Thanks for viewing.

A: 

Don't use NSTimer.

From the NS Timer reference

Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds.

I'm not an expert on the iPhone, but you probably need to look into animation on the platform to achieve the update interval you want.

JeremyP
+1  A: 

Redrawing the view is an expensive operation, which will decrease performance considerably. If you can instead, just have the background as an image view move across the background using [UIView beginAnimation... or using CAAnimations.

Ed Marty
The problem is i have to do it this way. It's not for an end application, its for a performance comparison with another technology.
toc777
Well, I think your comparison is done, then, isn't it? "Terrible performance" seems to be a good enough comparison for most ;)
Ed Marty
I know i don't need it updating 30 fps for a performance comparison but i would like to get it working anyway (just for show) because this is how it is running on the other technology.
toc777