Search code examples
c#xamarinxamarin.formsgesturepan

Xamarin Forms Pan gesture speed


is there any chance to change the speed of Pan gesture in Xamarin Forms?

That's my OnPanUpdated function and Clamp function:

void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
    switch (e.StatusType)
    {
        case GestureStatus.Started:
            StartX = (1 - AnchorX) * Width;
            StartY = (1 - AnchorY) * Height;
            break;

        case GestureStatus.Running:
            AnchorX = Clamp(1 - (StartX + (e.TotalX)) / Width, 0, 1);
            AnchorY = Clamp(1 - (StartY + (e.TotalY)) / Height, 0, 1);
            break;
    }
}

private T Clamp<T>(T value, T minimum, T maximum) where T : IComparable
{
    if (value.CompareTo(minimum) < 0)
      return minimum;
    else if (value.CompareTo(maximum) > 0)
      return maximum;
    else
      return value;
}

I tried to change every parameter in that function. Nothing really works. When I move my finger on the screen, image is moving but too slow for me. Can someone give me an advice or a solutions to make it move faster?


Solution

  • You can not change the frequency of which the Pan gesture is called, but you can apply a "stepping" factor to the movement:

    var panFactor = 2.5;
    AnchorX = Clamp(1 - (StartX + (e.TotalX * panFactor)) / Width, 0, 1);
    AnchorY = Clamp(1 - (StartY + (e.TotalY * panFactor)) / Height, 0, 1);
    

    Of course, this will cause the pan to "jump" by that factor.

    I would normally apply a logarithmic scale (i.e. simple hardcoded list of 10 doubles from 1.0 to 5.0 as calc'ing a true log scale at runtime is way overkill) to the factor based upon the length of time the pan is in Running state, the direction of the Pan is same/constant, and speed of the pan is constant.

    • If the pan is interrupted (Completed)
    • Or the pan direction changes (X|Y average-delta changes signs),

    Reset the factor back to 1 and start anew when the pan is Started again.

    • If the "speed" decreases (the X|Y average-delta is smaller than the last X samples)

    Reset the factor back to the prior log entry, i.e. start slowing down the pan speed, setting the factor back toward 1.0