Created
March 20, 2017 02:59
-
-
Save adityawibisana/4bf6df9aaa09f19d6685d9088b2876d1 to your computer and use it in GitHub Desktop.
fix Animate from and to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Animate(bool isDown, bool useDelay=true) | |
{ | |
Storyboard storyboard = new Storyboard(); | |
storyboard.SpeedRatio = useDelay ? 4 : 100000; | |
// translate the children | |
int i = 0; | |
foreach (Image image in rotationContainer.Children) | |
{ | |
//x translation | |
int fromIndex = (int) image.Tag; | |
int toIndex = fromIndex; | |
if (isDown) | |
{ | |
toIndex++; | |
if (toIndex>=itemsAmount) | |
{ | |
toIndex = 0; | |
} | |
} | |
else | |
{ | |
toIndex--; | |
if (toIndex<0) | |
{ | |
toIndex = itemsAmount - 1; | |
} | |
} | |
image.Tag = toIndex; | |
DoubleAnimation translateAnimationX = new DoubleAnimation() | |
{ | |
From = positions[fromIndex].X, | |
To = positions[toIndex].X | |
}; | |
Storyboard.SetTarget(translateAnimationX, image); | |
Storyboard.SetTargetProperty(translateAnimationX, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)")); | |
storyboard.Children.Add(translateAnimationX); | |
//y translation | |
DoubleAnimation translateAnimationY = new DoubleAnimation() | |
{ | |
From = positions[fromIndex].Y, | |
To = positions[toIndex].Y | |
}; | |
Storyboard.SetTarget(translateAnimationY, image); | |
Storyboard.SetTargetProperty(translateAnimationY, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)")); | |
storyboard.Children.Add(translateAnimationY); | |
i++; | |
int imageTag = (int)image.Tag; | |
if (imageTag>=-1 && imageTag <=visibleItems) | |
{ | |
image.Visibility = Visibility.Visible; | |
} | |
} | |
storyboard.Completed += ((sen, args) => | |
{ | |
animationRunning = false; | |
foreach (Image image in rotationContainer.Children) | |
{ | |
int imageTag = (int)image.Tag; | |
if (imageTag < 0 && imageTag>=visibleItems) | |
{ | |
image.Visibility = Visibility.Hidden; | |
} | |
} | |
}); | |
storyboard.Begin(); | |
animationRunning = true; | |
if (!isDown) currentIndex++; | |
else currentIndex--; | |
if (currentIndex >= itemsAmount) | |
currentIndex = 0; | |
else if (currentIndex==-1) | |
{ | |
currentIndex = itemsAmount - 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment