由於您的動畫似乎是重複循環往復幾個較小的子動畫序列,你可以跟蹤你在動畫循環,其中在任何時候,停止動畫時,您的應用程序變爲無效,並在您的應用再次激活時重新啓動動畫。
[編輯:示例代碼和解釋下面加入。]
在ViewController.m
:
@interface ViewController()
{
int numStages; // 4 * 3 = 12 in our example (4 different text labels,
// with 3 substages (alpha ramping up, alpha constant, and alpha ramping down) for each)
int globalStage; // varies from 0 to numStages - 1. 0 initially
bool animationIsActive;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
numStages = 4 * 3; // 4 * 3 = 12 in our example (4 different text labels,
// with 3 substages for each text label's appearance (alpha ramping up, alpha constant, and alpha ramping down))
globalStage = 0; // varies from 0 to numStages - 1. 0 initially
animationIsActive = NO;
self.myLabel.alpha = 0.0;
}
- (void)animateStage:(int)stage {
NSLog(@"animateStage called with argument stage = %d", stage);
// make a note in our instance variable of where we need to restart
// the animation THE NEXT TIME if it is interrupted or paused
// during the current animation:
globalStage = (stage + 1) % numStages;
self.myLabel.text = [NSString stringWithFormat:@"Text %d", (stage/3) + 1];
switch (stage % 3) {
case 0: // ramp up alpha from 0 to 1
{
[UIView animateWithDuration:0.3 animations:^{
self.myLabel.alpha = 1.0;
} completion:^(BOOL finished) {
// only proceed to next stage if the animation is supposed to be active:
if (animationIsActive) {
[self animateStage:globalStage];
}
}];
}
break;
case 1: // keep alpha constant at 1 (see comment below)
{
[UIView animateWithDuration:2.7 animations:^{
self.myLabel.alpha = 0.99999; // changing the 0.99999 to 1.0 causes
// this stage to be short-circuited. probably because iOS realizes
// that alpha is not really changing in this stage and, being too clever
// by half, decides to skip this stage altogether. but 0.99999 is
// as close to 1.0 as makes no difference.
} completion:^(BOOL finished) {
// only proceed to next stage if the animation is supposed to be active:
if (animationIsActive) {
[self animateStage:globalStage];
}
}];
}
break;
case 2: // ramp down alpha from 1 to 0
{
[UIView animateWithDuration:0.3 animations:^{
self.myLabel.alpha = 0.0;
} completion:^(BOOL finished) {
// only proceed to next stage if the animation is supposed to be active:
if (animationIsActive) {
[self animateStage:globalStage];
}
}];
}
break;
default:
break;
}
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"viewWillAppear: called");
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(willResignActive:)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
}
- (void)viewDidDisappear:(BOOL)animated
{
NSLog(@"viewDidDisappear: called");
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
}
- (void)didBecomeActive:(NSNotification *)notification
{
NSLog(@"view controller's didBecomeActive: called");
// start the animation is we are stopped
if (!animationIsActive)
{
NSLog(@"animation being (re)started at stage %d", globalStage);
[self animateStage:globalStage];
animationIsActive = YES;
}
}
- (void)willResignActive:(NSNotification *)notification
{
NSLog(@"view controller's willResignActive: called");
// stop the animation
NSLog(@"animation being stopped at stage %d", globalStage);
animationIsActive = NO;
}
@end
- 我分解和 「扁平」 動畫成4 * 3 = 12更好的控制階段。
- 實例變量
globalStage
和animationIsActive
用於跟蹤我們在動畫中的位置以及動畫是否正在運行。
- 在
viewWillAppear:
我們希望在應用程序變爲活動或不活動狀態時收到通知。當這些事件發生時,我們的方法didBecomeActive:
或willResignActive:
被調用。這兩種方法是我們(重新)開始和停止我們的動畫的地方。
- 不要忘記在
viewDidDisappear:
註銷兩個UIApplication
通知。
我該怎麼做..我不知道如何阻止它? –
我已經添加了一些示例代碼和詳細的解釋到我的原始答案。請享用! – inwit
謝謝你......那正是我需要的..... –