2015-06-04 23 views
1

我有一個秒錶類,它在其tick方法中定期更新視圖。該方法通過觸發在Apple Watch上不可靠的performSelectorAfterDelay

[self performSelector:@selector(tick) withObject:self afterDelay:0.1f]; 

這工作正常的iPhone視圖獲取更新大約。每十分之一秒。

現在我想在Apple Watch上使用相同的類來實現此目的。當我啓動秒錶時,視圖會得到更新。但是,2秒後,幀率下降,秒錶變得無法使用。

這裏是整個tick方法:

- (void)tick { 
if (!self.running) return; 
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate]; 
NSTimeInterval elapsed = currentTime - startTime; 
if (countdown) { 
    curTime = originalTime - (elapsed * 1000); 
}else { 
    curTime = originalTime + (elapsed * 1000); 
    curTime -= offset; 
} 

[self.delegate stopwatch:self tick:curTime]; 
long thisThousand = curTime/1000; 

if (curTime > 0 || !countdown) { 
    if (thisThousand != lastThousand) { 
     long seconds = curTime/1000; 
     if (countdown) seconds++; 
     [self.delegate stopwatch:self fullSecond:seconds]; 
    } 
    lastThousand = curTime/1000; 
    [self performSelector:@selector(tick) withObject:self afterDelay:0.1f]; 
} else { 
    curTime = 0; 
    [self.delegate stopwatch:self fullSecond:0]; 
    [self.delegate stopwatchStoped:self]; 
    [self stopTimer]; 
} 
} 

如何得到那個工作任何想法。還是另一種解決方案?

非常感謝!

編輯: 用戶界面只是一個簡單的標籤,而不是一個奇特的動畫。標籤應該設置爲0:00:00.1至0:00:00.2等等。

回答

-1

如果你從這個方法更新視圖(我假設[self.delegate stopwatch:self fullSecond:seconds]更新視圖?)如果是,那麼你應該看看CoreAnimation。動畫更好地設計來處理視圖隨着時間的變化(畢竟這是一個動畫!)。如果您正在製作第二隻手的動畫,則可以創建一個動畫,使其「打勾」並簡單地重複該過程。還有一些委託方法可以在動畫完成時通知您,您可以使用該方法執行任何其他計算。

Look here CoreAnimation上的文檔。

performSelector:afterDelay:另一種方法是使用NSTimer

// timer is an iVar 
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(tick) userInfo:nil repeats:YES]; 

// then to stop the timer 
[timer invalidate]; 

我首先建議CoreAnimation。

+0

值得指出的是Core Animation目前在WatchKit中不可用。 – bgilham

+0

啊。這很煩人。希望它會很快,然後你都可以upvote這個優秀的答案。 –

相關問題