开发者

UIView drawing problem

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
    开发者_如何学JAVA    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...


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜