• 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= 
  • 相关阅读:
    直拍反手拉球引拍位置及发力技巧
    话说多球 --  乒在民间
    直板横打不稳定,总是出界的可能原因 -- 乒在民间
    【hihocoder 1424】 Asa's Chess Problem(有源汇上下界网络流)
    【HDU 6036】Division Game (NTT+数学)
    【hdu 4658】Integer Partition (无序分拆数、五边形数定理)
    【hdu 5628】Clarke and math (Dirichlet卷积)
    【hdu6188】Duizi and Shunzi(贪心)
    【hdu6186】CS Course(前缀后缀异或)
    【hdu6185】Covering(骨牌覆盖)
  • 原文地址:https://www.cnblogs.com/malingbo/p/2298081.html
Copyright © 2020-2023  润新知