Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to make a ScrollViewer "bouncy"?

Im using a WPF ScrollViewer to host some controls. I'd really like it to interact like on a touch device where it slowly eases back into place when you pull it too far.

It doesn't have a scrollbar - I have manual mouse scrolling with click and drag using this code:

Point currentPoint = e.GetPosition(this);

// Determine the new amount to scroll.
Point delta = new Point(scrollStartPoint.X - currentPoint.X, scrollStartPoint.Y - currentPoint.Y);

if (Math.Abs(delta.X) < PixelsToMoveToBeConsideredScroll &&
    Math.Abs(delta.Y) < PixelsToMoveToBeConsideredScroll)
    return;

scrollTarget.X = scrollStartOffset.X + delta.X;
scrollTarget.Y = scrollStartOffset.Y + delta.Y;

// Scroll to the new position.
sv.ScrollToHorizontalOffset(scrollTarget.X);
sv.ScrollToVerticalOffset(scrollTarget.Y);

Is there an easy way to do this? Currently it just acts like a normal scrolling textbox and will neither pull outside its normal range, nor slowly ease back.

like image 499
kvanbere Avatar asked Nov 13 '22 06:11

kvanbere


1 Answers

If you are working on a Touch device, look at the Microsoft SurfaceScrollViewer (http://msdn.microsoft.com/en-us/library/microsoft.surface.presentation.controls.surfacescrollviewer.aspx). It already has this behaviour built in.

Getting Touch interaction right is tricky, easier to find someone else who's already done it. ;-)

like image 62
RJ Lohan Avatar answered Nov 16 '22 03:11

RJ Lohan