private void OnTouchMoved(Point currentPosition) { var elapsedTime = DateTime.Now - InitialTime; if (elapsedTime <= InitialDelayTime) { return; } //var deltaFromInitialPosition = currentPosition - InitialPosition; //if (deltaFromInitialPosition.Length < InitialScrollingTriggerThreshold) //{ // return; //} var delta = currentPosition - PreviousPosition; var magnitude = delta.Length * ScrollResolution; if (ScrollOrientation == Orientation.Vertical) { if (currentPosition.Y < PreviousPosition.Y) { magnitude *= -1; } } else { if (currentPosition.X < PreviousPosition.X) { magnitude *= -1; } } var newOffset = (ScrollOrientation == Orientation.Vertical ? AssociatedScrollViewer.VerticalOffset : AssociatedScrollViewer.HorizontalOffset) + magnitude; if (ScrollOrientation == Orientation.Vertical) { AssociatedScrollViewer.ScrollToVerticalOffset(newOffset); } else { AssociatedScrollViewer.ScrollToHorizontalOffset(newOffset); } PreviousPosition = currentPosition; }
private void CompositionTarget_Rendering(object sender, EventArgs e) { lock (inertiaSync) { if (CurrentForce <= 0) { return; } var currentTime = DateTime.Now; var elapsedTime = currentTime - LastUpdate; CurrentVelocity = CurrentForce / BodyMass; var dueDisplacement = CurrentVelocity * (Double)elapsedTime.TotalMilliseconds; if (CurrentForceDirection == ForceDirection.Down) { dueDisplacement *= -1; } var newOffset = (ScrollOrientation == Orientation.Vertical ? AssociatedScrollViewer.VerticalOffset : AssociatedScrollViewer.HorizontalOffset) + dueDisplacement; if (Double.IsNaN(newOffset)) { return; } if (ScrollOrientation == Orientation.Vertical) { AssociatedScrollViewer.ScrollToVerticalOffset(newOffset); } else { AssociatedScrollViewer.ScrollToHorizontalOffset(newOffset); } CurrentForce = Math.Max(0, CurrentForce - ((Double)elapsedTime.TotalMilliseconds * CurrentForce * Friction)); if (CurrentForce < 0.0001) { CurrentForce = 0; } LastUpdate = DateTime.Now; } }