Question

I have a method running performSelector on itself every .01 seconds in order to constantly update an image.

-(void)setNeeds

[mag setNeedsDisplay];
[vc setNeedsDisplay];
[na setNeedsDisplay];
[wave setNeedsDisplay];
[mon setNeedsDisplay];

[self performSelector:@selector(setNeeds)
           withObject:NULL
           afterDelay:.01];

This updates just fine when there is normal user interaction. However, when a user slides values around on the UIViewPicker, all updates are paused until the interaction stops. The same thing happens with an NSTimer object set on repeat.

I'm hoping for a more consistent method of performing a continuous action but a solution to this problem would also be grand.

Was it helpful?

Solution

This is a fairly common question since an NSTimer created with scheduledTimerWith... may pause while there is user interaction. Here is a similar question. The performSelector:..afterDelay:.. methods use an NSTimer internally, as you would imagine. Basically when you use scheduledTimerWith... or performSelector:..afterDelay:.. your timer is scheduled in the run loop for NSDefaultRunLoopMode which may pause while the user is interacting with the application. The solution is to schedule the timer in the run loop yourself for CommonModes like so:

timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(setNeeds) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

If you are opposed to managing your own NSTimer for this function, then you can use the performSelector:withObject:afterDelay:inModes: function. The final argument is an array of run loop modes, in which you may put NSRunLoopCommonModes.

OTHER TIPS

marking everything as needs display every 1/100th of a second will not give you a framerate of 100fps, marking something as needs display only gives a view the opportunity to be drawn on the next pass through the main run-loop.

if drawing takes 1/10th of a second, then you will get 10fps...

plus this approach requires the setNeeds to be called, and if that call cycle is somehow interrupted it will never resume, you should use a NSTimer +scheduledTimer... call to make a recurring timer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top