第二十二章:动画(十八)-云栖社区-阿里云

超越高级动画方法你到目前为止看到的ConcurrentAnimations中的例子仅限于Scale和Rotate属性的动画,因此它们没有显示任何你无法做的事情。ViewExtensions类中的方法。

超越高级动画方法
你到目前为止看到的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。