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= 
  • 相关阅读:
    CentOS7 彻底关闭 IPV6
    查看 nodejs 安装包的相关指令
    npm 查看全局安装过的包
    更换 nodejs npm 镜像为 淘宝 镜像
    更改 Centos 6 的 yum 源
    Nodejs 实现 WebSocket 太容易了吧!!
    解决国内 NPM 安装依赖速度慢问题
    详解 HTML5 中的 WebSocket 及实例代码-做弹幕
    JSmpeg-用JavaScript编写的视频播放器
    适用于Centos6.x系统的15项优化脚本
  • 原文地址:https://www.cnblogs.com/malingbo/p/2298081.html
Copyright © 2011-2022 走看看