10. Xamarin中ViewExtension实现平移(包含

2017-02-15  本文已影响70人  shannoon

1. 平移动画

ViewExtension中的TranslateTo方法实现平移

/// <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结束,会执行此匿名函数
                    });
上一篇 下一篇

猜你喜欢

热点阅读