我有一个两部分动画,可以将一个物体滑入屏幕中间,暂停,然后滑动到某个位置,将其缩小为零。运行故事板时崩溃了,是不是可以在同一个故事板中对同一个对象进行两种不同的translatetransforms?
作为修复,我尝试将动画分成两个故事板,但是我遇到了另一个问题。当第一部分完成时,卡片会重置回角落的起始位置,就好像动画从未发生过一样。然后我的第二个故事板在几秒钟之后开始。如果该职位之后没有重置,那就没问题了。
所以如何让它在下面的一个故事板中运行,或者如果我将它分成两个故事板,则在完成第1部分动画后不重置位置。
以下代码仅在我从第2部分删除第二个translatetransform动画时才有效。
public void SendCardToPile(UIElement bigCard, UIElement targetElement)
{
this.ZoomDropShadowImage.Visibility = Visibility.Collapsed;
// setup
var _Scale = new ScaleTransform
{
ScaleX = 1.0,
ScaleY = 1.0,
CenterX = 0,
CenterY = 0
};
this.ZoomItemCard.Visibility = Visibility.Visible;
var _Translate = new TranslateTransform();
var _Group = new TransformGroup();
_Group.Children.Add(_Scale);
_Group.Children.Add(_Translate);
bigCard.RenderTransform = _Group;
// animate
var _Storyboard = new Storyboard { };
var _Storyboard2 = new Storyboard { };
var transform = targetElement.TransformToVisual(Application.Current.RootVisual as FrameworkElement);
Point absolutePosition = transform.Transform(new Point(0, 0));
// Part 1 **************************
// translate (location X)
var _TranslateAnimateX = new DoubleAnimation
{
BeginTime = TimeSpan.FromSeconds(0),
From = -(Application.Current.Host.Content.ActualHeight / 2),
To = (Application.Current.Host.Content.ActualHeight - 250) / 2,
Duration = TimeSpan.FromSeconds(.25)
};
_Storyboard.Children.Add(_TranslateAnimateX);
Storyboard.SetTarget(_TranslateAnimateX, _Translate);
Storyboard.SetTargetProperty(_TranslateAnimateX,
new PropertyPath(TranslateTransform.XProperty));
// translate (location Y)
var _TranslateAnimateY = new DoubleAnimation
{
BeginTime = TimeSpan.FromSeconds(0),
From = (Application.Current.Host.Content.ActualWidth - 400) / 2,
To = (Application.Current.Host.Content.ActualWidth - 400) / 2,
Duration = TimeSpan.FromSeconds(.25)
};
_Storyboard.Children.Add(_TranslateAnimateY);
Storyboard.SetTarget(_TranslateAnimateY, _Translate);
Storyboard.SetTargetProperty(_TranslateAnimateY,
new PropertyPath(TranslateTransform.YProperty));
// Part 2 **************************
double stopDelay = 2;
double disappearSpeed = .5;
// scale X
var _ScaleAnimateX = new DoubleAnimation
{
BeginTime = TimeSpan.FromSeconds(stopDelay),
To = 0.0,
From = 1.0,
Duration = TimeSpan.FromSeconds(disappearSpeed)
};
_Storyboard.Children.Add(_ScaleAnimateX);
Storyboard.SetTarget(_ScaleAnimateX, _Scale);
Storyboard.SetTargetProperty(_ScaleAnimateX,
new PropertyPath(ScaleTransform.ScaleXProperty));
// scale Y
var _ScaleAnimateY = new DoubleAnimation
{
BeginTime = TimeSpan.FromSeconds(stopDelay),
To = 0.0,
From = 1.0,
Duration = TimeSpan.FromSeconds(disappearSpeed)
};
_Storyboard.Children.Add(_ScaleAnimateY);
Storyboard.SetTarget(_ScaleAnimateY, _Scale);
Storyboard.SetTargetProperty(_ScaleAnimateY,
new PropertyPath(ScaleTransform.ScaleYProperty));
// translate (location X)
var _TranslateAnimateX2 = new DoubleAnimation
{
BeginTime = TimeSpan.FromSeconds(stopDelay),
To = absolutePosition.X,
Duration = TimeSpan.FromSeconds(disappearSpeed)
};
_Storyboard.Children.Add(_TranslateAnimateX2);
Storyboard.SetTarget(_TranslateAnimateX2, _Translate);
Storyboard.SetTargetProperty(_TranslateAnimateX2,
new PropertyPath(TranslateTransform.XProperty));
// translate (location Y)
var _TranslateAnimateY2 = new DoubleAnimation
{
BeginTime = TimeSpan.FromSeconds(stopDelay),
To = absolutePosition.Y,
Duration = TimeSpan.FromSeconds(disappearSpeed)
};
_Storyboard.Children.Add(_TranslateAnimateY2);
Storyboard.SetTarget(_TranslateAnimateY2, _Translate);
Storyboard.SetTargetProperty(_TranslateAnimateY2,
new PropertyPath(TranslateTransform.YProperty));
// finalize
//EventHandler eh = null;
//eh = (s, args) =>
//{
// _Storyboard.Completed -= eh;
//};
//_Storyboard.Completed += eh;
_Storyboard.Begin();
}
答案 0 :(得分:0)
我能够通过使用关键帧的单个故事板获得我想要的动画
public void SendCardToPile(UIElement bigCard, UIElement targetElement)
{
this.ZoomDropShadowImage.Visibility = Visibility.Collapsed;
double stopDelay = 2;
double disappearSpeed = .5;
// setup
var _Scale = new ScaleTransform
{
ScaleX = 1.0,
ScaleY = 1.0,
CenterX = 0,
CenterY = 0
};
this.ZoomItemCard.Visibility = Visibility.Visible;
var _Translate = new TranslateTransform();
var _Group = new TransformGroup();
_Group.Children.Add(_Scale);
_Group.Children.Add(_Translate);
bigCard.RenderTransform = _Group;
// animate
var _Storyboard = new Storyboard { };
var transform = targetElement.TransformToVisual(Application.Current.RootVisual as FrameworkElement);
Point absolutePosition = transform.Transform(new Point(0, 0));
// translate (location X)
DoubleAnimationUsingKeyFrames _TranslateAnimateX = new DoubleAnimationUsingKeyFrames();
System.Windows.Media.Animation.Storyboard.SetTarget(_TranslateAnimateX, _Translate);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(_TranslateAnimateX, new PropertyPath(TranslateTransform.XProperty));
_TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)),
Value = -(Application.Current.Host.Content.ActualHeight / 2),
});
_TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.25)),
Value = (Application.Current.Host.Content.ActualHeight - 250) / 2
});
_TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
Value = (Application.Current.Host.Content.ActualHeight - 250) / 2
});
_TranslateAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
Value = absolutePosition.X
});
_Storyboard.Children.Add(_TranslateAnimateX);
// translate (location Y)
DoubleAnimationUsingKeyFrames _TranslateAnimateY = new DoubleAnimationUsingKeyFrames();
System.Windows.Media.Animation.Storyboard.SetTarget(_TranslateAnimateY, _Translate);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(_TranslateAnimateY, new PropertyPath(TranslateTransform.YProperty));
_TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)),
Value = (Application.Current.Host.Content.ActualWidth - 400) / 2
});
_TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.25)),
Value = (Application.Current.Host.Content.ActualWidth - 400) / 2
});
_TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
Value = (Application.Current.Host.Content.ActualWidth - 400) / 2
});
_TranslateAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
Value = absolutePosition.Y
});
_Storyboard.Children.Add(_TranslateAnimateY);
// scale X
DoubleAnimationUsingKeyFrames _ScaleAnimateX = new DoubleAnimationUsingKeyFrames();
System.Windows.Media.Animation.Storyboard.SetTarget(_ScaleAnimateX, _Scale);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(_ScaleAnimateX, new PropertyPath(ScaleTransform.ScaleXProperty));
_ScaleAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
Value = 1.0
});
_ScaleAnimateX.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
Value = 0.0
});
_Storyboard.Children.Add(_ScaleAnimateX);
// scale Y
DoubleAnimationUsingKeyFrames _ScaleAnimateY = new DoubleAnimationUsingKeyFrames();
System.Windows.Media.Animation.Storyboard.SetTarget(_ScaleAnimateY, _Scale);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(_ScaleAnimateY, new PropertyPath(ScaleTransform.ScaleYProperty));
_ScaleAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay)),
Value = 1.0
});
_ScaleAnimateY.KeyFrames.Add(new LinearDoubleKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(stopDelay + disappearSpeed)),
Value = 0.0
});
_Storyboard.Children.Add(_ScaleAnimateY);
// finalize
//EventHandler eh = null;
//eh = (s, args) =>
//{
// _Storyboard.Completed -= eh;
//};
//_Storyboard.Completed += eh;
_Storyboard.Begin();
}