欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF实现左右移动(晃动)动画效果

程序员文章站 2022-04-29 12:38:16
本文实例为大家分享了wpf实现左右移动效果展示的具体代码,供大家参考,具体内容如下 实现控件或布局的左右移动(晃动)主要用到doubleanimation以及storyb...

本文实例为大家分享了wpf实现左右移动效果展示的具体代码,供大家参考,具体内容如下

实现控件或布局的左右移动(晃动)主要用到doubleanimation以及storyboard

布局代码为:

<canvas>
    <grid width="200" height="100" background="mediumaquamarine" name="groupboxarea" canvas.left="100" canvas.top="200"/>
    <button content="button" height="25" width="78" click="button_click"/>


</canvas>

后台代码为:

 private void button_click(object sender, routedeventargs e)
    {
      doubleanimation danimation = new doubleanimation();
      danimation.from = 100;//起点
      danimation.to = 280;//终点
      danimation.duration = new duration(timespan.fromseconds(0.5));//时间

      storyboard.settarget(danimation, groupboxarea);
      storyboard.settargetproperty(danimation, new propertypath(canvas.leftproperty));
      storyboard story = new storyboard();

      story.completed += new eventhandler(story_completed);//完成后要做的事
      //story.repeatbehavior = repeatbehavior.forever;//无限次循环,需要的自己加上
      story.children.add(danimation);
      story.begin();
    }
    void story_completed(object sender, eventargs e)
    {
      doubleanimation danimation = new doubleanimation();
      danimation.from = 280;//起点
      danimation.to = 100;//终点
      danimation.duration = new duration(timespan.fromseconds(0.5));//时间

      storyboard.settarget(danimation, groupboxarea);
      storyboard.settargetproperty(danimation, new propertypath(canvas.leftproperty));
      storyboard story = new storyboard();

      story.completed += new eventhandler(storycompleted);//完成后要做的事
      //story.repeatbehavior = repeatbehavior.forever;//无限次循环,需要的自己加上
      story.children.add(danimation);
      story.begin();
    }

    void storycompleted(object sender, eventargs e)
    {
      doubleanimation danimation = new doubleanimation();
      danimation.from = 100;//起点
      danimation.to = 200;//终点
      danimation.duration = new duration(timespan.fromseconds(0.5));//时间

      storyboard.settarget(danimation, groupboxarea);
      storyboard.settargetproperty(danimation, new propertypath(canvas.leftproperty));
      storyboard story = new storyboard();

      //story.completed += new eventhandler(storycompleted);//完成后要做的事
      //story.repeatbehavior = repeatbehavior.forever;//无限次循环,需要的自己加上
      story.children.add(danimation);
      story.begin();
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。