あなたはiOSの4+をターゲットにしている場合は、グランドセントラル派遣を使用することができます。
// Set the time, '33333333' nanoseconds in the future (33.333333ms)
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 33333333);
// Schedule our code to run
dispatch_after(time, dispatch_get_main_queue(), ^{
// your code to run here...
});
これは33.333333ms後にそのコードを呼び出します。
void DoWork(void *context);
void ScheduleWork() {
// Set the time, '33333333' nanoseconds in the future (33.333333ms)
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 33333333);
// Schedule our 'DoWork' function to run
// Here I pass in NULL for the 'context', whatever you set that to will
// get passed to the DoWork function
dispatch_after_f(time, dispatch_get_main_queue(), NULL, &DoWork);
}
void DoWork(void *context) {
// ...
// Do your work here, updating an on screen counter or something
// ...
// Schedule our DoWork function again, maybe add an if statement
// so it eventually stops
ScheduleWork();
}
そして、あなたはタイマーを開始したいときにだけScheduleWork();
を呼び出します。これはみかん取引ループになるだろうされている場合は、代わりにブロックの関数ポインタを使用して代わりにdispatch_after_f
機能を使用する場合があります繰り返しループの場合、私は個人的にはこれが上記のブロックメソッドより少しきれいだと思っていますが、一度の作業ではブロックメソッドを確実に使用しています。
詳細については、Grand Central Dispatch docsを参照してください。
_frameInterval_はiOS 10.0では廃止予定でしたので、_preferredFramesPerSecond_を使用してください: 'displayLink.preferredFramesPerSecond = 30.0;' for 30fps – WongWray