2016-08-22 118 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。我保證這將意味着更少的頭痛:)