2016-08-22 116 views
2

方法调用动画addSubview破坏动画迅速

for button in self.exploreButtonsArray { 
     button.exploreButtonExitAnimation() 
    } 

    setupDrinkExploreButtons() 

代码为动画:

func exploreButtonExitAnimation() { 
    UIView.animateWithDuration(random, delay: 0.0, options: [], animations: { 
     self.center.x -= 400 
     }, completion: {(value: Bool) in 
      self.removeFromSuperview() 
    }) 

} 

setupDrinkExploreButtons()方法调用.addSubview(exploreButton)并增加了一些按钮的容器。我的动画是否搞乱了,因为这些动作是异步执行的?

This is the very first view before any animation

How the view looks without calling setupDrinkExploreButtons()

What it looks like when calling setupDrinkExploreButtons()

第一图象:这是所有动画

第二图像之前的第一个观点:如何视图看起来不调用setupDrinkExploreButtons()。所有按钮优雅地移动到左边。

3图像:它看起来什么叫setupDrinkExploreButtons时像()

在你可以看到按钮不会消失,但第3图像,而不是他们似乎是“动画的”,以适应视图。他们似乎没有像他们应该向左边移动400个单位,而是突然出现了400个单位,然后向左移动以适应他们的原始位置?

+0

你可能想检查“随机”的值,看看他们在适当的范围值。 – Dasem

+0

@Dasem动画的问题不在于速度是随机的。我已经做了一些截图来演示问题是如何发生的。 – tryingtolearn

回答

0

我最好的猜测是你在self.exploreButtonsArray中使用旧的新视图的相同引用,或者在创建新视图时清空数组。如果你想发布你的代码,那么我可以看看。

但是,我有几个建议:

首先,这是做你的动画有点不可靠的方式。您将创建十几个动画块,根据时间的不同,它们可能会在稍微不同的时间开始和结束。这可能是也可能不是显而易见的,但这是一个坏习惯,并且效率较低。为了避免这个问题,你可以做这样的事情:

UIView.animateWithDuration(random, delay: 0.0, options: [], animations: { 
    for button in self.exploreButtonsArray { 
     // BTW this is also unreliable, I would strongly suggest using definite values when animating 
     button.center.x -= 400   
    } 
    }, completion: {(value: Bool) in 
     for button in self.exploreButtonsArray { 
     button.removeFromSuperview() 
    } 
}) 

其次,如果你分组视图这样在一起,这将是更好的把他们都在一个视图中,然后动态显示视图来代替。编码时,这对性能和简单性都更高效。

第三,似乎你正在尝试做一些非常简单的分页。如果你只是玩得开心,想要自己实现它,那就去吧!如果不是,您可能需要使用UIPageViewControllerUICollectionViewController。我保证这将意味着更少的头痛:)