10. Xamarin中ViewExtension实现平移(包含有弹簧的渐变效果)旋转缩放动画

1. 平移动画

ViewExtension中的TranslateTo方法实现平移

  • 具体参数四个
  • x:水平方向移动距离
  • y:垂直方向移动距离
  • length:平移动画所用时间,单位毫秒
  • easing:平移动画在移动过程中的动画方式,具体值可以看其枚举,有:慢进快出,慢进,慢出,弹簧效果进入,弹框效果退出等
/// <param name="view">The view to tanslate.</param>
    /// <param name="x">The x component of the final translation vector.</param>
    /// <param name="y">The y component of the final translation vector.</param>
    /// <param name="length">The duration of the animation in milliseconds.</param>
    /// <param name="easing">The easing of the animation.</param>
    /// <summary>Animates an elements TranslationX and TranslationY properties from their current values to the new values.</summary>
    /// <returns>To be added.</returns>
    /// <remarks>Translate to is particular useful for animations because it is applied post-layout. Translation animations will not conflict with managed layouts and thus are ideal for doing slide in/out style animations.</remarks>
    public static Task<bool> TranslateTo(this VisualElement view, double x, double y, uint length = 250, Easing easing = null)
    {
  • 示例:
Task.Delay(10000).ContinueWith((b) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    UnlockView.TranslateTo(animateDelta, 0,length:4000, easing: Easing.BounceOut);
                    Task.Delay(2000).ContinueWith((c) =>
                    {
                        ViewExtensions.CancelAnimations(UnlockView);
                        Task.Delay(3000).ContinueWith((a) =>
                        {
                            Device.BeginInvokeOnMainThread(() =>
                            {
                                UnlockView.TranslateTo(0, 0, easing: Easing.SinOut);
                            });
                        });
                    });
                });
            });

2. 动画过程中取消动画CancelAnimations

  ViewExtensions.CancelAnimations(UnlockView);

3. 此外还有其他动画效果,例如旋转,缩放等有时间再补充,看下图

Paste_Image.png

更详细信息请阅读官方ViewExtensions介绍,链接.

4. 动画结束后的监听,执行某些操作

动画会返回一个Task对象,然后调用ContinueWith就可以添加此Task结束后执行的任务

UnlockView.TranslateTo(0, 0, easing: Easing.BounceOut).ContinueWith((_) =>
                    {
                        // 添加动画执行完毕后的代码
                        // task结束,会执行此匿名函数
                    });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,980评论 0 23
  • 总能遇见朋友让我推荐书,我总是要想一下才回复。其实看书这件事和一个人的口味是一样的,都是非常私人化的。有人嗜辣,有...
    夏筠若阅读 146评论 1 1
  • 你是一株鸳鸯菊 花开的日子很短 最美也就那么一两天 你为谁开 谁赏你的花容 谁嗅你的芬芳 你是一株鸳鸯菊 是黎明里...
    情长纸短阅读 179评论 0 2
  • 成长就是比昨天强大 1.概念丰富 以前不了解的概念,现在有了,比如成长率,快速入门,价值。这些东西可以指导方向。 ...
    郑向飞的空间阅读 212评论 0 0
  • map()方法介绍 map()是Array提供的方法,通过接收一个函数作为传入参数,对数组中每个元素进行函数变换得...
    清风沐沐阅读 2,487评论 0 1