Friday, October 8, 2010

Using NSThread implement Timer


用內建的NSTimer時,會因view中加入以UIScrollView為base的物件時,而導致托拉View時產生Timer hold住的情形。此時我們需要自已利用 NSThread + NSThread 來實作我們自已的Timer以解決上述問題。其例子如下:



-(void)startTimer{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector:@selector(launchTimer) toTarget:self withObject:nil];
[pool release];

-(void)launchTimer{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread setThreadPriority:1.0];
continuePlaying = YES;
while (continuePlaying) { // loop until cancelled
[self performSelectorOnMainThread:@selector(updateTimer) withObject:nil waitUntilDone:NO];
[NSThread sleepForTimeInterval:1.0];
}
[pool release];
}
Dragon

No comments:

Post a Comment