ManipulationDelta和动画

时间:2011-08-01 15:24:38

标签: wpf animation

我是WPF的新手。我实现了全屏应用程序,网格可以通过鼠标移动(拖放式)。如果网格控件拖出屏幕边界,则会返回默认状态并使用简单动画。问题是当网格返回时它可以再移动了!提供了一些代码段:

public partial class MenuCard : UserControl, ITouchObject, INotifyPropertyChanged
{
    ...
    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("X", typeof(double), typeof(MenuCard), new UIPropertyMetadata(0.0, OnPosXChanged, CourceXValue));
    ...
    private static void OnPosXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var owner = (MenuCard) d;
        Grid rootGrid = (Grid)Application.Current.MainWindow.FindName("rootGrid");
        Point actual = owner.TransformToAncestor(rootGrid).Transform(new Point(owner.X, owner.Y));
        Point topLeft = owner.TransformToAncestor(rootGrid).Transform(new Point(0, 0));
        Point bottomRight = owner.TransformToAncestor(rootGrid).Transform(new Point(owner.ActualWidth, owner.ActualHeight));

        if (actual.X + (bottomRight.X - topLeft.X) <= border || actual.X >= SystemParameters.PrimaryScreenWidth - border)
        {
            ReturnToDefault(owner);
        }
    }
    ...
    private static void ReturnToDefault(MenuCard owner)
    {
        DoubleAnimation yAnimation = new DoubleAnimation();
        yAnimation.From = owner.Y;
        yAnimation.DecelerationRatio = 0.5;
        yAnimation.To = (double)YProperty.DefaultMetadata.DefaultValue;
        owner.BeginAnimation(MenuCard.YProperty, yAnimation);

        DoubleAnimation xAnimation = new DoubleAnimation();
        xAnimation.From = owner.X;
        xAnimation.DecelerationRatio = 0.5;
        xAnimation.To = (double)XProperty.DefaultMetadata.DefaultValue;
        owner.BeginAnimation(MenuCard.XProperty, xAnimation);
    }
}

主要课程:

public partial class MainWindow : Window
{

    private void CanvasManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        var p = GetSender(e.ManipulationContainer as FrameworkElement);     
        if (p != null)
        {
            ManipulationDelta md = e.DeltaManipulation;
            p.X += md.Translation.X;
            p.Y += md.Translation.Y;
            p.Angle += md.Rotation;
            p.ScaleX *= md.Scale.X;
            p.ScaleY *= md.Scale.Y;
        }
        e.Handled = true;
    }

    private ITouchObject GetSender(FrameworkElement element)
    {
        while (true)
        {
            if (element.Parent == null)
            {
                return null;
            }
            if (element is ITouchObject)
            {
                return element as ITouchObject;
            }
            element = element.Parent as FrameworkElement;
        }
    }
}

我没有想法。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

确保将FillBehavior属性添加到所有动画...

xAnimation.FillBehavior = FillBehavior.Stop;

这可能会导致网格回到动画之前的相同位置......

您可以在动画结束时手动设置属性..

像这样的事情

这里给予动画的价值......

yAnimation.To =(double)YProperty.DefaultMetadata.DefaultValue;

在动画结束时直接设置相同的值...

MenuCard.YProperty =(double)YProperty.DefaultMetadata.DefaultValue;