2011-01-12 2 views
2

私は無期限のUIViewをアニメーション化するために、次のコードを使用しています:私のアプリケーションがバックグラウンドになったとき、私の不定なUIViewアニメーションが止まるのはなぜですか?

#define DEFAULT_ANIM_SPPED 0.6 
#define INFINATE_VALUE 1e100f 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationRepeatAutoreverses:YES]; 
[UIView setAnimationRepeatCount:INFINATE_VALUE]; 
[UIView setAnimationDuration:DEFAULT_ANIM_SPPED]; 
CGRect tempFrame=myView.frame; 
tempFrame.origin.y -= 30; 
myView.frame=tempFrame; 
[UIView commitAnimations]; 

私のアプリケーションがバックグラウンドに行き、その後、私はそれに戻った場合は、このようなすべてのアニメーションは現在停止されています。なぜこれが起こっていますか?

答えて

10

通常、アプリケーションの実行はバックグラウンドになると完全に中断されます。アプリケーションがアクティブのままであっても、画面will be haltedに描画し、何も(位置追跡、VoIPの、またはその他の理由で)ときに、背景の状態へのアプリケーションの移動:あなたの窓とビューを更新

は避けてください。 バックグラウンドでは、 アプリケーションのウィンドウとビューは表示されないので、 を更新しないようにしてください。 ウィンドウとビューオブジェクトを操作すると、バックグラウンドで アプリケーションが終了することはありませんが、 アプリケーションがフォアグラウンドに移動するまで、この 作業を延期する必要があります。実際に

、あなたがバックグラウンドでのOpenGL ESコンテキストに描画しようとした場合、アプリケーションwill immediately crash

はどんなのOpenGL ESをしないでくださいは コードから呼び出します。 EAGLContextオブジェクトを作成したり、バックグラウンドで実行している のいずれかの種類のOpenGL ES描画コマンドを発行しないでください。これらの コールを使用すると、アプリケーションはすぐに終了する になります。

一般に、アプリケーションの動きのアニメーションをバックグラウンドで一時停止し、アプリケーションがフォアグラウンドになったらアニメーションを再開することをお勧めします。これは-applicationDidEnterBackground:-applicationWillEnterForeground:代理人の方法で処理するか、UIApplicationDidEnterBackgroundNotificationUIApplicationWillEnterForegroundNotificationの通知を聞いて処理できます。

+0

有益な情報ありがとうございます。 –

+0

IMOこのパターンは、あまりにも複雑すぎるアニメーションを作り出します。 – Jonny

-2

iOsは、アプリケーションがバックグラウンドになるとコードの実行を停止します。 Check this out.

0

@BradLarsonが彼のanswerで示唆したように、私は私のビューコントローラにUIApplicationDidEnterBackgroundNotificationUIApplicationWillEnterForegroundNotificationを処理するためにNSNotificationObserverを追加しました。

- (void) addNotificationObserver { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; 
} 

- (void) removeNotificationObserver { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; 
} 

- (void) applicationDidEnterBackground:(NSNotification *)notification { 
    // went to background 
} 

- (void) applicationWillEnterForeground:(NSNotification *)notification { 
    // comes to foreground 
} 

- (void) viewDidLoad { 
    [super viewDidLoad]; 
    [self addNotificationObserver]; 
} 

- (void) dealloc { 
    [self removeNotificationObserver]; 
} 
関連する問題