以编程方式创建缓动函数 C#

发布于 2024-11-28 10:14:45 字数 1314 浏览 6 评论 0原文

我正在学习如何使用 C# 创建动画。因此,如果我要对对象的边距进行动画处理,那么我将使用以下方法对其边距进行动画处理:

test(someObject, FrameworkElement.MarginProperty);

/// <summary>
/// animate margin of an object
/// </summary>
/// <param name="target">what object do you whant to animate?</param>
/// <param name="property">what property do you want to animate</param>
public void test(DependencyObject target, DependencyProperty property)
{
    ThicknessAnimation animation = new ThicknessAnimation();
    animation.To = new Thickness(0,0,0,0);   // final value

    //animation.From = new Thickness(50,50,50,50);

    //make animation last 5 seconds
    animation.BeginTime = TimeSpan.FromSeconds(0);
    animation.Duration = TimeSpan.FromSeconds(5);

    // set the ease function
    BounceEase b = new BounceEase();
    animation.EasingFunction = b;

    //note that I would like to add an easeIn function

    //start animating
    Storyboard.SetTarget(animation, target);  // what object will be animated?
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    sb.Begin();
}

请注意,我能够创建一个缓动函数。但是如果我想创建一个 EaseInOut 缓动函数怎么办?为了使用 EaseInOut 方法为对象设置动画,我必须在测试方法中添加什么。

I am learning how to create animations with C#. so if I am animating the margin of an object for example then I would use the following method to animate its margin:

test(someObject, FrameworkElement.MarginProperty);

/// <summary>
/// animate margin of an object
/// </summary>
/// <param name="target">what object do you whant to animate?</param>
/// <param name="property">what property do you want to animate</param>
public void test(DependencyObject target, DependencyProperty property)
{
    ThicknessAnimation animation = new ThicknessAnimation();
    animation.To = new Thickness(0,0,0,0);   // final value

    //animation.From = new Thickness(50,50,50,50);

    //make animation last 5 seconds
    animation.BeginTime = TimeSpan.FromSeconds(0);
    animation.Duration = TimeSpan.FromSeconds(5);

    // set the ease function
    BounceEase b = new BounceEase();
    animation.EasingFunction = b;

    //note that I would like to add an easeIn function

    //start animating
    Storyboard.SetTarget(animation, target);  // what object will be animated?
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    sb.Begin();
}

note that I am able to create a ease function. But what if I want to create an EaseInOut easing function. what do I have to add to my test method in order to animate the object using an EaseInOut approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2024-12-05 10:14:45

我想我终于让我的方法起作用了,以便为对象的边缘设置动画。

MyStoryboards.Animations a = new MyStoryboards.Animations();

// set the ease function
BounceEase b = new BounceEase();
b.Bounces = 5;
b.Bounciness = 1;
b.EasingMode = EasingMode.EaseIn;

a.animThickness(tv, FrameworkElement.MarginProperty, tv.Margin, new Thickness(), TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5), b);

……

public void animThickness(DependencyObject target, DependencyProperty property, Thickness from, Thickness to, TimeSpan beginTime, TimeSpan duration , IEasingFunction e)
{
    ThicknessAnimation animation = new ThicknessAnimation();
    animation.To = to;   // final value

    animation.From = from;

    animation.BeginTime = beginTime;
    animation.Duration = duration;

    animation.EasingFunction = e;

    //start animating
    Storyboard.SetTarget(animation, target);  // what object will be animated?
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    sb.Begin();
}

I guess I finally got my method working in order to animate the margin of an object.

MyStoryboards.Animations a = new MyStoryboards.Animations();

// set the ease function
BounceEase b = new BounceEase();
b.Bounces = 5;
b.Bounciness = 1;
b.EasingMode = EasingMode.EaseIn;

a.animThickness(tv, FrameworkElement.MarginProperty, tv.Margin, new Thickness(), TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5), b);

...

...

public void animThickness(DependencyObject target, DependencyProperty property, Thickness from, Thickness to, TimeSpan beginTime, TimeSpan duration , IEasingFunction e)
{
    ThicknessAnimation animation = new ThicknessAnimation();
    animation.To = to;   // final value

    animation.From = from;

    animation.BeginTime = beginTime;
    animation.Duration = duration;

    animation.EasingFunction = e;

    //start animating
    Storyboard.SetTarget(animation, target);  // what object will be animated?
    Storyboard.SetTargetProperty(animation, new PropertyPath(property)); // what property will be animated
    Storyboard sb = new Storyboard();
    sb.Children.Add(animation);
    sb.Begin();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文