zoukankan      html  css  js  c++  java
  • ScrollView动画滚动

    想实现类似iphone的界面滚动效果,查了一下,容器使用ScrollView,里面放一个StackPanel,StackPanel里面放入需要滚动的控件,宽度高度与ScrollView相同,然后隐藏滚动条即可,不过问题出来了,设置滚动只能用ScrollToVerticalOffset和ScrollToHorizontalOffset(offset)方法,那就没法用StoryBoard了,怎么办呢?使用DispatcherTimer 

    View Code
    DispatcherTimer dispatcherTimer= new DispatcherTimer();

                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

                dispatcherTimer.Interval = TimeSpan.FromMilliseconds(50); //重复间隔

                dispatcherTimer.Start();

     这样就定义了一个计时器,每50ms运行一次,下面我们只需要改变全局变量的值,计时器就可以实时显示动画了。计时器事件函数为:

    void dispatcherTimer_Tick(object sender, EventArgs e)
            {
               
                switch(defaultDirect)
                {
                    case Direct.back:
                        if (xPreoffset > xoffset-speed*1.5)
                        {//从xPreoffset移动到xoffset
                            this.scrollViewer1.ScrollToHorizontalOffset(xPreoffset);
                            xPreoffset -= speed;

                        }else
                        this.scrollViewer1.ScrollToHorizontalOffset(xoffset);
                        break;
                    case Direct.forwork:
                        if (xPreoffset < xoffset + speed * 1.5)
                        {
                            this.scrollViewer1.ScrollToHorizontalOffset(xPreoffset);
                            xPreoffset += speed;
                           
                        }else
                        this.scrollViewer1.ScrollToHorizontalOffset(xoffset);
                        break;
                    case Direct.down:
                        this.scrollViewer1.ScrollToVerticalOffset(yoffset);
                        break;
                    case Direct.up:
                        this.scrollViewer1.ScrollToVerticalOffset(yoffset);
                        break;
                }

            }

    定义变量:

    定义变量
    public enum Direct
        {
            none,up,down,forwork,back
        }

            private double speed = 50;//速度
            private double xPreoffset = 0;//移动前的位置
            private double xoffset = 0;//移动到的位置
            private double yoffset = 0;
            private Direct defaultDirect = Direct.none;

     然后放一个button用来修改变量:

    前进/后退按钮
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                defaultDirect = Direct.forwork;
                xoffset += scrollViewer1.Width;
                if (xoffset > scrollcontainer.Width) xoffset = scrollcontainer.Width;
                
            }

            private void button2_Click(object sender, RoutedEventArgs e)
            {
                defaultDirect = Direct.back;
                xoffset -= scrollViewer1.Width;
                if (xoffset < 0) xoffset = 0;
               
            }

     这样就完成了,具体的文件在这里

    DispatcherTimer dispatcherTimer= 
  • 相关阅读:
    LeetCode_441. Arranging Coins
    LeetCode_437. Path Sum III
    Spearman秩相关系数和Pearson皮尔森相关系数
    Spark MLlib 之 Basic Statistics
    Spark MLlib 之 Naive Bayes
    Spark MLlib Data Type
    maven 下载 源码和javadoc 命令
    Hadoop的数据输入的源码解析
    Spark相关错误汇总
    Spark External Datasets
  • 原文地址:https://www.cnblogs.com/malingbo/p/2298081.html
Copyright © 2011-2022 走看看