ورود

View Full Version : سوال: آیا میشه زمان KeyTime ها رو در storyboard تغییر داد ؟



mdssoft
چهارشنبه 04 خرداد 1390, 21:10 عصر
با سلام.

من در پروژم ، Storyboard زیر رو دارم :


<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="top">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="topLeft">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="middle">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bottomLeft">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bottom">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="topRight">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.995"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bottomRight">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.995"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="topBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="topLeftBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="middleBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bottomLeftBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bottomBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="topRightBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="bottomRightBlur">
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>

چطور می تونم KeyTime های DoubleAnimationUsingKeyFrames رو از طریق کد C# تغییر بدم ؟

ممنون.

مهدی فرزاد
چهارشنبه 04 خرداد 1390, 23:06 عصر
سلام
من از یک روش استفاده میکنم که با یک مثال این روش رو توضیح میدم
فرض کنیم StoryBoard ما این باشه
<Storyboard x:Key="OnClick1" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="Window">
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="900"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>

حالا با کد زیر زمان رو نصف میکنم
System.Windows.Media.Animation.Storyboard sb = FindResource("OnClick1") as System.Windows.Media.Animation.Storyboard;
sb.SpeedRatio = 2;

کد زیر اونو دوبرابر میکنه
System.Windows.Media.Animation.Storyboard sb = FindResource("OnClick1") as System.Windows.Media.Animation.Storyboard;
sb.SpeedRatio = 0.5;

راه دوم
var sb = FindResource("OnClick1") as System.Windows.Media.Animation.Storyboard;
var ani = sb.Children[0] as System.Windows.Media.Animation.DoubleAnimationUsin gKeyFrames;
var keyFrames = ani.KeyFrames[0];

keyFrames.KeyTime = System.Windows.Media.Animation.KeyTime.FromTimeSpa n(new TimeSpan(0, 0, 0, 20));

نکته ممکنه تعداد DoubleAnimationUsingKeyFrames بیش از یک مورد باشه ( مثل مثال خودتون)
مثلا
<Storyboard x:Key="OnClick1" >
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Childr en)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="button1">
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1.279"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Childr en)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="button1">
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1.514"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Childr en)[3].(TranslateTransform.X)" Storyboard.TargetName="button1">
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="12"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Childr en)[3].(TranslateTransform.Y)" Storyboard.TargetName="button1">
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="9"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>

کدش به این شکل میشه
var sb = FindResource("OnClick1") as System.Windows.Media.Animation.Storyboard;

var ani = sb.Children[0] as System.Windows.Media.Animation.DoubleAnimationUsin gKeyFrames;
var keyFrames = ani.KeyFrames[0];
keyFrames.KeyTime = System.Windows.Media.Animation.KeyTime.FromTimeSpa n(new TimeSpan(0, 0, 0, 20));

ani = sb.Children[1] as System.Windows.Media.Animation.DoubleAnimationUsin gKeyFrames;
keyFrames = ani.KeyFrames[0];
keyFrames.KeyTime = System.Windows.Media.Animation.KeyTime.FromTimeSpa n(new TimeSpan(0, 0, 0, 20));

ani = sb.Children[2] as System.Windows.Media.Animation.DoubleAnimationUsin gKeyFrames;
keyFrames = ani.KeyFrames[0];
keyFrames.KeyTime = System.Windows.Media.Animation.KeyTime.FromTimeSpa n(new TimeSpan(0, 0, 0, 20));

ani = sb.Children[3] as System.Windows.Media.Animation.DoubleAnimationUsin gKeyFrames;
keyFrames = ani.KeyFrames[0];
keyFrames.KeyTime = System.Windows.Media.Animation.KeyTime.FromTimeSpa n(new TimeSpan(0, 0, 0, 20));

یا به شکل خلاصه
var sb = FindResource("OnClick1") as System.Windows.Media.Animation.Storyboard;
foreach (var ani in sb.Children)
{
var keyFrames = (ani as System.Windows.Media.Animation.DoubleAnimationUsin gKeyFrames).KeyFrames[0];
keyFrames.KeyTime = System.Windows.Media.Animation.KeyTime.FromTimeSpa n(new TimeSpan(0, 0, 0, 20));
}

امیدوارم منظور رو رسونده باشم
البته من پیشنهاد میکنم در مواقعی که نیاز دارید انیمیشن متغییر داشته باشید کلا اونو در code Behind بنویسید
در لینک زیر یک مثال از این مسئله وجود داره
http://barnamenevis.org/showthread.php?279004-%D9%85%D8%B4%DA%A9%D9%84-%D8%A8%D8%A7-%D8%A7%DB%8C%D8%AC%D8%A7%D8%AF-%D8%A7%D9%86%DB%8C%D9%85%DB%8C%D8%B4%D9%86-Window-%D8%AF%D8%B1-%D8%AF%DB%8C%DA%A9%D8%B4%D9%86%D8%B1%DB%8C