ScrollView动画滚动
想实现类似iphone的界面滚动效果,查了一下,容器使用ScrollView,里面放一个StackPanel,StackPanel里面放入需要滚动的控件,宽度高度与ScrollView相同,然后隐藏滚动条即可,不过问题出来了,设置滚动只能用ScrollToVerticalOffset和ScrollToHorizontalOffset(offset)方法,那就没法用StoryBoard了,怎么办呢?使用DispatcherTimer
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DispatcherTimer dispatcherTimer= new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(50); //重复间隔
dispatcherTimer.Start();
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;
}
}
{
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;
}
}
定义变量:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
{
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用来修改变量:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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;
}
{
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=