我正在尝试开发拖放式磁贴交换运动。一切正常,但过渡不顺畅。任何人都可以帮助我平滑过渡。
<Canvas x:Name="baseCanvas" MouseDown="Canvas_MouseDown" MouseUp="Canvas_MouseUp" MouseMove="Canvas_MouseMove">
<Label x:Name="label1" Canvas.Left="{Binding XPosition1}" MouseLeftButtonDown="Label_MouseLeftButtonDown" Content="1" Width="40" Height="40" Background="Gray" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="25" Canvas.Top="47" Panel.ZIndex="2"/>
<Label x:Name="label2" Canvas.Left="{Binding XPosition2}" MouseLeftButtonDown="Label_MouseLeftButtonDown" Content="2" Width="40" Height="40" Background="Gray" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="25" Canvas.Top="47" Panel.ZIndex="2"/>
<Label x:Name="label3" Canvas.Left="{Binding XPosition3}" MouseLeftButtonDown="Label_MouseLeftButtonDown" Content="3" Width="40" Height="40" Background="Gray" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="25" Canvas.Top="47" Panel.ZIndex="2"/>
<Label x:Name="label4" Canvas.Left="{Binding XPosition4}" MouseLeftButtonDown="Label_MouseLeftButtonDown" Content="4" Width="40" Height="40" Background="Gray" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="25" Canvas.Top="47" Panel.ZIndex="2"/>
<Label x:Name="label5" Canvas.Left="{Binding XPosition5}" MouseLeftButtonDown="Label_MouseLeftButtonDown" Content="5" Width="40" Height="40" Background="Gray" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="25" Canvas.Top="47" Panel.ZIndex="2"/>
</Canvas>
它就像windows 8任务栏瓷砖一样工作。拖动过渡是平滑的,因为我根据鼠标移动位置不断更新绑定的XPosition。
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
switch ((e.Source as Label).Content.ToString())
{
case "1":
movingLocationHelper(Mouse.GetPosition(baseCanvas).X - offset, 1);
break;
case "2":
movingLocationHelper(Mouse.GetPosition(baseCanvas).X - offset, 2);
break;
case "3":
movingLocationHelper(Mouse.GetPosition(baseCanvas).X - offset, 3);
break;
case "4":
movingLocationHelper(Mouse.GetPosition(baseCanvas).X - offset, 4);
break;
case "5":
movingLocationHelper(Mouse.GetPosition(baseCanvas).X - offset, 5);
break;
}
}
}
private void movingLocationHelper(double position, int source)
{
switch (source)
{
case 1:
XPosition1 = position;
break;
case 2:
XPosition2 = position;
break;
case 3:
XPosition3 = position;
break;
case 4:
XPosition4 = position;
break;
case 5:
XPosition5 = position;
break;
}
position += 20;
if (position <= position2 - 1)
{
swapItems(1, source);
}
else if (position2 <= position && position <= position3 - 1)
{
swapItems(2, source);
}
else if (position3 <= position && position <= position4 - 1)
{
swapItems(3, source);
}
else if (position4 <= position && position <= position5 - 1)
{
swapItems(4, source);
}
else if (position5 <= position)
{
swapItems(5, source);
}
}
private void swapItems(int positionIndex, int movingItem)
{
int movingItemPosition = 0;
int existingItem = 0;
for (int i = 0; i < positionArray.Length; i++)
{
if(positionArray[i] == movingItem)
{
movingItemPosition = i;
}
}
existingItem = positionArray[positionIndex - 1];
positionArray[positionIndex - 1] = movingItem;
positionArray[movingItemPosition] = existingItem;
if (movingItem == 1)
{
XPosition2 = findPosition(2);
XPosition3 = findPosition(3);
XPosition4 = findPosition(4);
XPosition5 = findPosition(5);
}
else if (movingItem == 2)
{
XPosition1 = findPosition(1);
XPosition3 = findPosition(3);
XPosition4 = findPosition(4);
XPosition5 = findPosition(5);
}
else if (movingItem == 3)
{
XPosition1 = findPosition(1);
XPosition2 = findPosition(2);
XPosition4 = findPosition(4);
XPosition5 = findPosition(5);
}
else if (movingItem == 4)
{
XPosition1 = findPosition(1);
XPosition2 = findPosition(2);
XPosition3 = findPosition(3);
XPosition5 = findPosition(5);
}
else if (movingItem == 5)
{
XPosition1 = findPosition(1);
XPosition2 = findPosition(2);
XPosition3 = findPosition(3);
XPosition4 = findPosition(4);
}
}
因此,当谈到swapItems时,转换并不顺利,因为我通过更新新的XPosition位置来交换项目。我需要帮助动画交换部分。感谢。