2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2017-03-07 22:56:24 +03:00
|
|
|
using Xamarin.Forms.Internals;
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
namespace Xamarin.Forms.Platform.Android
|
|
|
|
{
|
|
|
|
internal class PinchGestureHandler
|
|
|
|
{
|
|
|
|
double _pinchStartingScale = 1;
|
|
|
|
|
|
|
|
public PinchGestureHandler(Func<View> getView)
|
|
|
|
{
|
|
|
|
GetView = getView;
|
|
|
|
}
|
|
|
|
|
|
|
|
Func<View> GetView { get; }
|
|
|
|
|
|
|
|
// A View can have at most one pinch gesture, so we just need to look for one (or none)
|
2017-09-15 21:49:19 +03:00
|
|
|
PinchGestureRecognizer PinchGesture => GetView()?.GestureRecognizers.OfType<PinchGestureRecognizer>()
|
|
|
|
.FirstOrDefault();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
public bool OnPinch(float scale, Point scalePoint)
|
|
|
|
{
|
|
|
|
View view = GetView();
|
|
|
|
|
|
|
|
if (view == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PinchGestureRecognizer pinchGesture = PinchGesture;
|
|
|
|
if (pinchGesture == null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
var scalePointTransformed = new Point(scalePoint.X / view.Width, scalePoint.Y / view.Height);
|
2017-09-15 21:49:19 +03:00
|
|
|
pinchGesture.SendPinch(view, 1 + (scale - 1) * _pinchStartingScale, scalePointTransformed);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnPinchEnded()
|
|
|
|
{
|
|
|
|
View view = GetView();
|
|
|
|
|
|
|
|
if (view == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PinchGestureRecognizer pinchGesture = PinchGesture;
|
2017-09-15 21:49:19 +03:00
|
|
|
pinchGesture?.SendPinchEnded(view);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool OnPinchStarted(Point scalePoint)
|
|
|
|
{
|
|
|
|
View view = GetView();
|
|
|
|
|
|
|
|
if (view == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PinchGestureRecognizer pinchGesture = PinchGesture;
|
|
|
|
if (pinchGesture == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
_pinchStartingScale = view.Scale;
|
|
|
|
|
|
|
|
var scalePointTransformed = new Point(scalePoint.X / view.Width, scalePoint.Y / view.Height);
|
|
|
|
|
2017-09-15 21:49:19 +03:00
|
|
|
pinchGesture.SendPinchStarted(view, scalePointTransformed);
|
2016-03-22 23:02:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|