天天看點

第二十二章:動畫(十八)

超越進階動畫方法

你到目前為止看到的ConcurrentAnimations中的例子僅限于Scale和Rotate屬性的動畫,是以它們沒有顯示任何你無法做的事情。

ViewExtensions類中的方法。 但是因為您可以通路實際的回調方法,是以您可以在回調期間執行任何操作。

這是一個動畫,您可以使用它來訓示您的應用程式正在執行可能需要一些時間才能完成的操作。 您沒有顯示ActivityIndicator,而是選擇顯示一個長度從0到10重複增加的句點字元串。這兩個值被指定為Animation構造函數的參數。 回調方法将目前值轉換為整數,以便與其中一個鮮為人知的字元串構造函數一起使用,以構造具有該點數的字元串:

public partial class ConcurrentAnimationsPage : ContentPage
{
    bool keepAnimation5Running = false;
    __
    void OnButton5Clicked(object sender, EventArgs args)
    {
        Animation animation = 
                    new Animation(v => dotLabel.Text = new string('.', (int)v), 0, 10);
        animation.Commit(this, "Animation5", 16, 3000, null, 
                        (v, cancelled) => dotLabel.Text = "",
                        () => keepAnimation5Running);
        keepAnimation5Running = true;
    }
    void OnTurnOffButtonClicked(object sender, EventArgs args)
    {
        keepAnimation5Running = false;
    }
    __
}           

OnButton5Clicked方法通過将keepAnimation5Running字段設定為true來結束,并且Commit方法中的重複回調傳回該值。 動畫将一直運作,直到keepAnimation5Running設定為false,這就是下一個Button所做的事情。

此技術與取消動畫之間的差別在于此技術不會立即結束動畫。 隻有在動畫到達其結束值(在這種情況下為10)後才會調用重複回調,是以在keepAnimation5Running設定為false之後動畫可以繼續運作幾乎三秒鐘。

ConcurrentAnimations程式中的最後一個示例通過将其設定為Color.FromHsla方法建立的Color值來設定頁面的BackgroundColor屬性,其中色調值範圍為0到1.此動畫提供了掃描彩虹顔色的效果:

public partial class ConcurrentAnimationsPage : ContentPage
{
   __
   void OnButton6Clicked(object sender, EventArgs args)
   {
      new Animation(callback: v => BackgroundColor = Color.FromHsla(v, 1, 0.5), 
               start: 0, 
               nd: 1).Commit(owner: this, 
                     name: "Animation6", 
                     length: 5000, 
                     finished: (v, c) => BackgroundColor = Color.Default);
   }
}           

此代碼使用命名參數,是以說明了另一種文法變體,用于執行個體化Animation對象并在其上調用Commit。

繼續閱讀