问题恢复 UIVIew 动画:ios7? [英] Issue Resuming UIVIew Animation: ios7?

查看:23
本文介绍了问题恢复 UIVIew 动画:ios7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当视图加载时,我通过调用方法 animateSettings 为 UIImageView 设置动画,该方法执行以下操作:

I am animating a UIImageView up and down when the view loads by calling the method animateSettings which does the following:

- (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];
}

问题是当我退出应用程序并返回时,动画冻结.我明白为什么会这样,但我似乎不知道如何恢复它.

The problem is that when I exit the app and return, the animation freezes. I understand why that is, but I can't seem to figure out how to resume it.

我在 viewDidLoad 方法中设置了 NSNotifications 以在视图变为活动状态或进入后台时通知我.执行以下操作,我调用 animateSettings 方法或 removeAnimation 方法.

I've set NSNotifications in the viewDidLoad method to notify me when the view becomes active or it enters the background. Doing the following I call the animateSettings method or removeAnimation method.

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

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

这是我的 removeAnimation 方法

Here's my removeAnimation method

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

问题:即使方法被正确调用(NSLogging 工作),动画仍然被冻结.

Problem: Even though the method are being called correctly (NSLogging works) the animation is still frozen.

推荐答案

第一次开始制作动画(我们称之为1)动画代码说:

The first time you start animating (let's call it 1) animation code says:

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

现在假设您进入后台并返回.再次调用您的动画代码(我们称之为 2):

Now let's say you go into the background and return. Your animation code is called again (let's call this 2):

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

好的,但是self.waterImageView现在在哪里,在2期间?它已经 at CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75) - 因为那是你在1期间把它放在哪里!

Okay, but where is self.waterImageView right now, during 2? It is already at CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75) - because that is where you put it during 1!

因此什么都不会发生,因为如果 animatable 属性的值没有变化,就没有动画.

Therefore nothing happens, because there is no animation if there is no change in the value of the animatable property.

知道了这一点,解决方案就显而易见了.当您进入后台时,或者如果动画因任何原因停止,将视图放回原处.现在,当您再次开始动画时,视图可以在某处设置动画.

Knowing this, the solution is obvious. When you go into the background, or if the animation is stopped for any reason, put the view back where it was to start with. Now when you start the animation again, the view has somewhere to animate to.

这篇关于问题恢复 UIVIew 动画:ios7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆