2014-03-29 59 views
2

我动画一个UIImageView上下时通过调用其执行以下操作方法animateSettings认为负载:问题恢复UIVIEW动画:ios7?

- (void)animateSettings { 

    NSLog(@"Animate Settings"); 
    [UIView animateWithDuration:1.0f 
         delay:0.0f 
        options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewKeyframeAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) 
       animations:^{ 
        self.waterImageView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75); 
       } 
       completion:nil]; 
} 

的问题是,当我退出程序和返回,动画冻结。我明白这是为什么,但我似乎无法弄清楚如何恢复它。

我已经在viewDidLoad方法中设置了NSNotifications,以便在视图变为活动状态或进入后台时通知我。做以下我称为animateSettings方法或removeAnimation方法。

[[NSNotificationCenter defaultCenter]addObserver:self 
             selector:@selector(animateSettings) 
              name:UIApplicationDidBecomeActiveNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter]addObserver:self 
             selector:@selector(removeAnimation) 
              name:UIApplicationDidEnterBackgroundNotification 
              object:nil]; 

这里是我的removeAnimation方法

- (void)removeAnimation { 
    NSLog(@"Remove Animation"); 
    [self.waterImageView.layer removeAllAnimations]; 
} 

问题:即使方法正确调用(NSLogging作品)动画仍然冻结。

+0

当您进入后台时,不需要调用'removeAllAnimations'。它是由系统调用的。 – matt

回答

3

第一次启动动画(我们称之为)动画代码说:

self.waterImageView.center = 
    CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75); 

现在让我们假设你进入的背景和回报。您的动画代码被再次调用(我们称之为):

self.waterImageView.center = 
    CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75); 

好吧,但哪里self.waterImageView现在,在?这已经是CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75) - 因为这是你在期间的地方!

因此没有任何反应,因为如果在动画属性的值中没有更改,则没有动画。

知道了这一点,解决方案是显而易见的。当您进入背景时,或者因任何原因停止动画时,将视图放回到以开头的位置。现在,当你再次开始动画时,视图就有了动画的地方。

+0

谢谢马特。但是,请记住,我正在自动反转动画并不断循环。我也使用UIViewAnimationOptionBeginFromCurrentState作为选项,但它不会恢复飞行中的动画。也许我错了,但如果确实如此,它应该自动反向,正确吗? – KingPolygon

+0

您在动画中分配的位置是视图真正呈现的位置;动画只是表示层正在做的事情。在飞行中恢复无法工作,因为没有机上动画可以恢复 - 您将其删除! (在任何情况下,系统都会将其删除。) – matt

+0

另请参阅我的书中对这种动画的讨论:http://www.apeth.com/iOSBook/ch17.html#SECblockbasedviewanimation – matt