Xcode / iPad animateWithDuration背景/前景

时间:2012-02-29 15:56:42

标签: xcode ipad

我在UIViewController中有一个循环动画,在viewDidAppear函数中调用:

[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                 animations:^{ 
                     _introImage.center = CGPointMake(INTRO_IMAGE_X_END, INTRO_IMAGE_Y);

                 } 
                 completion:^(BOOL finished){
                     _introImage.center = CGPointMake(INTRO_IMAGE_X_START, INTRO_IMAGE_Y);
                 }];

当我将应用程序放入后台并将其带回前景时,动画将停止。为什么这样,我该怎么做才能重新启动动画?

1 个答案:

答案 0 :(得分:1)

你可以把它放在它自己的方法中,并从viewDidAppear调用该方法,也可以在appDelegate.m中的applicationDidBecomeActive中调用

-(void)animate {
[UIView animateWithDuration:8.0 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                 animations:^{ 
                     _introImage.center = CGPointMake(INTRO_IMAGE_X_END, INTRO_IMAGE_Y);

                 } 
                 completion:^(BOOL finished){
                     _introImage.center = CGPointMake(INTRO_IMAGE_X_START, INTRO_IMAGE_Y);
                 }];

}

为了从“viewDidAppear”触发方法,用一行调用方法,如下所示:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self animate];
}

当应用程序变为活动状态时,从应用程序委托中激活动画:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self.viewcontroller animate];
}

这就是假设您可能还想将viewDidAppear用于其他事情。将动画分成它自己的方法可以让你专门调用它,而不是在viewDidAppear中运行所有内容。