Inheritance: Windows.UI.Xaml.Controls.UserControl
コード例 #1
0
ファイル: MainPage.xaml.cs プロジェクト: YouthLin/2048UWP
        private void MoveAnimate(Tile tile, int direction, int distance)
        {
            //使用代码创建动画:
            //https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.media.animation.storyboard.aspx

            // 创建平移动画
            TranslateTransform moveTransform = new TranslateTransform();
            //设置起始坐标
            moveTransform.X = 0;
            moveTransform.Y = 0;
            //关联对象与动画
            tile.RenderTransform = moveTransform;
            //设置动画时间
            Duration duration = new Duration(TimeSpan.FromSeconds(1));
            // 创建xy方向的动画
            DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
            DoubleAnimation myDoubleAnimationY = new DoubleAnimation();
            //设置动画时间
            myDoubleAnimationX.Duration = duration;
            myDoubleAnimationY.Duration = duration;
            //创建故事板
            Storyboard moveStoryboard = new Storyboard();
            moveStoryboard.Duration = duration;
            //把xy方向的动画添加到故事板
            moveStoryboard.Children.Add(myDoubleAnimationX);
            moveStoryboard.Children.Add(myDoubleAnimationY);
            //设置xy动画的目标是平移动画
            Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
            Storyboard.SetTarget(myDoubleAnimationY, moveTransform);
            //设置动画要修改的属性是X、Y坐标
            Storyboard.SetTargetProperty(myDoubleAnimationX, "X");
            Storyboard.SetTargetProperty(myDoubleAnimationY, "Y");
            //设置结束坐标
            switch (direction)
            {
                case LEFT: myDoubleAnimationX.To = -tile.ActualWidth * distance; break;
                case UP: myDoubleAnimationY.To = -tile.ActualHeight * distance; break;
                case RIGHT: myDoubleAnimationX.To = tile.ActualWidth * distance; break;
                case DOWN: myDoubleAnimationY.To = tile.ActualHeight * distance; break;
            }
            //Canvas.ZIndex Attached Property
            //https://msdn.microsoft.com/zh-cn/library/windows/apps/windows.ui.xaml.controls.canvas.zindex.aspx
            tile.SetValue(Canvas.ZIndexProperty, 999);
            //开始动画
            moveStoryboard.Begin();
        }