I'm new to UWP/WinRT. In my project user can drag an image, and if he drags it into a valid position - it stays there, if its in an invalid position - it moves back to the start. I've done all of it except that last one. Currently I have a function which is called from PointerReleased
callback function of an image:
void MainPage::slowly_move_to(Image& timg, Point32_t to) { // Point32_t is just a struct of double X and Y
double tx = BoardCanvas().GetLeft(timg), ty = BoardCanvas().GetTop(timg), tenthx = (to.X - tx) / 10, tenthy = (to.Y - ty) / 10;
for (int cnt = 0; cnt < 8; cnt++, tx += tenthx, ty += tenthy) {
BoardCanvas().SetLeft(timg, tx);
BoardCanvas().SetTop(timg, ty);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // to be sure that I'll see it moving through 9 points and not just teleporting from a to b
}
BoardCanvas().SetLeft(timg, to.X);
BoardCanvas().SetTop(timg, to.Y);
}
It works perfectly except I don't see any process, it looks like teleportation in a second after I released pointer, I assume its because canvas
redraws only after PointerReleased
execution ends. So how to make canvas
redraw after each move of the image in cycle?
I tried CompositionAnimation from this comment, it works for Opacity
, Scale
, etc. just as I want it to work for Canvas.Top
and Canvas.Left
, but for them it just crashes the program with UnhandledException
. My new code:
void MainPage::slowly_move_to(Image& timg, Point32_t to){
auto compos = Window::Current().Compositor();
auto anim = compos.CreateScalarKeyFrameAnimation();
anim.InsertKeyFrame(1.f, (float)to.X);
anim.Duration(TimeSpan::duration(chrono::seconds(1)));
anim.Target(hstring(L"Canvas.Left"));
timg.StartAnimation(anim);
}
I can't figure out how to animate 2 properties at once, but it would be a great start to make it work for 1 at a time.