2011-03-17 19 views
1

我有一些動畫要在某個時間點播放。那個時間是由在backgroud中運行的某個線程決定的。現在我想播放動畫,無論用戶在哪個屏幕上。這裏是屏幕層次結構。不管任何屏幕播放動畫iphone

login - > homescreen(線程從viewdidload開始運行)。

從主屏幕用戶可以導航到任何其他屏幕,我希望動畫播放。現在我在主屏幕中有一個animationviewcontrller成員變量,我通過分配和初始化以及在動畫完成時推送和彈出來調用它。而且它只有10%的時間。我已經嘗試performselectoronmainthread和它仍然是一樣的。

我如何重新設計我的代碼,以便在屏幕上播放。

這裏是我的代碼爲我的線程。

[self startTimerThread]; // in viewdidload 

-(void)startTimerThread 
{ 
    homescreenthread = [[NSThread alloc] initWithTarget:self selector:@selector(setupTimerThread) object:nil]; 
    [homescreenthread start]; 
} 
-(void)setupTimerThread 
{ 
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; 
    NSTimer* timer = [NSTimer timerWithTimeInterval:5 
              target:self 
              selector:@selector(findnewmessages) 
              userInfo:nil 
              repeats:YES]; 
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; 
    [runLoop addTimer:timer forMode:NSRunLoopCommonModes]; 
    //[runLoop addTimer:timer forModes:NSRunLoopCommonModes]; 
    [runLoop run]; 
    [pool release]; 
} 

-(void)findnewmessages 
{//finding message from server here. i am using the following request 
    NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
     NSString *replyString = [[NSString alloc] initWithBytes:[serverReply bytes] length:[serverReply length] encoding: NSASCIIStringEncoding]; 

//我省略了很多其他代碼。 }

+0

解釋更多關於你爲什麼從主屏幕開始一個線程以及該線程正在做什麼的問題,需要在主線程上播放動畫。 – Jasarien 2011-03-17 14:17:56

+0

我有一個線程,因爲它不斷地輪詢服務器,以檢查是否在後臺的任何消息。如果它發現任何消息它將消息插入數據庫(sqlite),並應該讓用戶現在與動畫。 – Rajashekar 2011-03-17 17:26:55

+0

您是否考慮過使用定時器(並因此使用runloop而不是線程)以期望的時間間隔執行輪詢,然後使用異步'NSURLConnection's?這將消除線程以及任何可能對您的動畫產生不利影響的可能性。 – Jasarien 2011-03-17 17:55:08

回答

0

使用應用程序的委託來顯示動畫視圖。

id appDelegate = [UIApplication sharedApplication].delegate; 
[appDelegate.m_window addSubview:viewController.view]; 

此外,如果你綁在屏幕上繪製任何東西,它應該在主線程上完成。

請讓我知道,如果它幫助。

+0

謝謝。我會試着去找你。 – Rajashekar 2011-03-17 17:27:36

+0

感謝夥伴它的工作 – Rajashekar 2011-03-23 06:23:08