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

超越高級動畫方法
你到目前爲止看到的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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章