我正在尝试垂直滚动一系列矩形。每个矩形与下一个矩形有固定的距离。第一个矩形不得低于屏幕顶部 10 像素,而最后一个矩形不得高于文本框 20 像素。换句话说,我在模仿 Windows Phone 中的 SMS 应用程序。
理论上,下面的方法应该动态地滚动矩形,虽然它确实如此,但在某些情况下,一些矩形彼此之间的距离比它们应该的更近(最终重叠)。当屏幕上的轻弹缓慢时,效果似乎被放大了。
private void Flick()
{
int toMoveBy = (int)flickDeltaY;
//flickDeltaY is assigned in the HandleInput() method as shown below
//flickDeltaY = s.Delta.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
for (int i = 0; i < messages.Count; i++)
{
ChatMessage message = messages[i];
if (i == 0 && flickDeltaY > 0)
{
if (message.Bounds.Y + flickDeltaY > 10)
{
toMoveBy = 10 - message.Bounds.Top;
break;
}
}
if (i == messages.Count - 1 && flickDeltaY < 0)
{
if (message.Bounds.Bottom + flickDeltaY < textBox.Top - 20)
{
toMoveBy = textBox.Top - 20 - message.Bounds.Bottom;
break;
}
}
}
foreach (ChatMessage cm in messages)
{
Vector2 target = new Vector2(cm.Bounds.X, cm.Bounds.Y + toMoveBy);
Vector2 newPos = Vector2.Lerp(new Vector2(cm.Bounds.X, cm.Bounds.Y), target, 0.5F);
float omega = 0.05f;
if (Vector2.Distance(newPos, target) < omega)
{
newPos = target;
}
cm.Bounds = new Rectangle((int)newPos.X, (int)newPos.Y, cm.Bounds.Width, cm.Bounds.Height);
}
}
我真的不了解向量,所以如果这是一个愚蠢的问题,我深表歉意。