public partial class TouchRecognizer: IDisposable { public event EventHandler<TouchActionEventArgs> OnTouchActionInvoked; public partial void Dispose(); }
public partial class TouchRecognizer : IDisposable { Android.Views.View androidView; Func<double, double> fromPixels; int[] twoIntArray = new int[2]; private Point _oldscreenPointerCoords;
public TouchRecognizer(Android.Views.View view) { this.androidView = view; if (view != null) { fromPixels = view.Context.FromPixels; view.Touch += OnTouch; } }
public partial void Dispose() { androidView.Touch -= OnTouch; }
void OnTouch(object sender, Android.Views.View.TouchEventArgs args) { var senderView = sender as Android.Views.View; var motionEvent = args.Event; var pointerIndex = motionEvent.ActionIndex; var id = motionEvent.GetPointerId(pointerIndex); senderView.GetLocationOnScreen(twoIntArray); var screenPointerCoords = new Point(twoIntArray[0] + motionEvent.GetX(pointerIndex), twoIntArray[1] + motionEvent.GetY(pointerIndex));
switch (args.Event.ActionMasked) { case MotionEventActions.Down: case MotionEventActions.PointerDown: InvokeTouchActionEvent(this, id, TouchActionType.Pressed, screenPointerCoords, true); break;
case MotionEventActions.Move: for (pointerIndex = 0; pointerIndex < motionEvent.PointerCount; pointerIndex++) { id = motionEvent.GetPointerId(pointerIndex);
senderView.GetLocationOnScreen(twoIntArray);
screenPointerCoords = new Point(twoIntArray[0] + motionEvent.GetX(pointerIndex), twoIntArray[1] + motionEvent.GetY(pointerIndex));
if (IsOutPit(senderView, screenPointerCoords)) { if (_oldscreenPointerCoords != default) { InvokeTouchActionEvent(this, id, TouchActionType.Exited, screenPointerCoords, true); _oldscreenPointerCoords=default; }
case MotionEventActions.Up: case MotionEventActions.Pointer1Up: InvokeTouchActionEvent(this, id, TouchActionType.Released, screenPointerCoords, false); break;
void InvokeTouchActionEvent(TouchRecognizer touchEffect, int id, TouchActionType actionType, Point pointerLocation, bool isInContact) { touchEffect.androidView.GetLocationOnScreen(twoIntArray); double x = pointerLocation.X - twoIntArray[0]; double y = pointerLocation.Y - twoIntArray[1]; var point = new Point(fromPixels(x), fromPixels(y)); OnTouchActionInvoked?.Invoke(this, new TouchActionEventArgs(id, actionType, point, isInContact)); }
public partial void Dispose() { windowsView.PointerEntered -= View_PointerEntered; windowsView.PointerPressed -= View_PointerPressed; windowsView.Tapped -=View_Tapped; windowsView.PointerMoved -= View_PointerMoved; windowsView.PointerReleased -= View_PointerReleased; windowsView.PointerExited -= View_PointerEntered; windowsView.PointerCanceled -= View_PointerCancelled; } private void View_Tapped(object sender, TappedRoutedEventArgs args) { //var windowsPoint = args.GetPosition(sender as UIElement); //Point point = new Point(windowsPoint.X, windowsPoint.Y); //InvokeTouchActionEvent(TouchActionType.Pressed, point, 0, true);
} private void View_PointerEntered(object sender, PointerRoutedEventArgs args) { Point point = GetPoint(sender, args); var id = args.Pointer.PointerId; var isInContact = args.Pointer.IsInContact; InvokeTouchActionEvent(TouchActionType.Entered, point, id, isInContact); }
private void View_PointerPressed(object sender, PointerRoutedEventArgs args) { Point point = GetPoint(sender, args); var id = args.Pointer.PointerId; var isInContact = args.Pointer.IsInContact; InvokeTouchActionEvent(TouchActionType.Pressed, point, id, isInContact); (sender as FrameworkElement).CapturePointer(args.Pointer); }
private void View_PointerMoved(object sender, PointerRoutedEventArgs args) { Point point = GetPoint(sender, args); var id = args.Pointer.PointerId; var isInContact = args.Pointer.IsInContact; InvokeTouchActionEvent(TouchActionType.Moved, point, id, isInContact); }
private void View_PointerReleased(object sender, PointerRoutedEventArgs args) { Point point = GetPoint(sender, args); var id = args.Pointer.PointerId; var isInContact = args.Pointer.IsInContact; InvokeTouchActionEvent(TouchActionType.Released, point, id, isInContact); }
private void View_PointerExited(object sender, PointerRoutedEventArgs args) { Point point = GetPoint(sender, args); var id = args.Pointer.PointerId; var isInContact = args.Pointer.IsInContact; InvokeTouchActionEvent(TouchActionType.Exited, point, id, isInContact); }
private void View_PointerCancelled(object sender, PointerRoutedEventArgs args) { Point point = GetPoint(sender, args); var id = args.Pointer.PointerId; var isInContact = args.Pointer.IsInContact; InvokeTouchActionEvent(TouchActionType.Cancelled, point, id, isInContact); }
private void InvokeTouchActionEvent(TouchActionType touchActionType, Point point, uint id, bool isInContact) { OnTouchActionInvoked?.Invoke(this, new TouchActionEventArgs(id, touchActionType, point, isInContact));
}
private static Point GetPoint(object sender, PointerRoutedEventArgs args) { var pointerPoint = args.GetCurrentPoint(sender as UIElement); Windows.Foundation.Point windowsPoint = pointerPoint.Position; Point point = new Point(windowsPoint.X, windowsPoint.Y); return point; } }
var handler = this.Handler; if (handler != null) { #if WINDOWS touchRecognizer = new TouchRecognizer(handler.PlatformView as Microsoft.UI.Xaml.FrameworkElement); touchRecognizer.OnTouchActionInvoked += TouchRecognizer_OnTouchActionInvoked; #endif #if ANDROID touchRecognizer = new TouchRecognizer(handler.PlatformView as Android.Views.View); touchRecognizer.OnTouchActionInvoked += TouchRecognizer_OnTouchActionInvoked;
#endif
#if IOS|| MACCATALYST touchRecognizer = new TouchRecognizer(handler.PlatformView as UIKit.UIView); touchRecognizer.OnTouchActionInvoked += TouchRecognizer_OnTouchActionInvoked;
(handler.PlatformView as UIKit.UIView).UserInteractionEnabled = true; (handler.PlatformView as UIKit.UIView).AddGestureRecognizer(touchRecognizer); #endif }
(handler.PlatformView as UIKit.UIView).UserInteractionEnabled = false; (handler.PlatformView as UIKit.UIView).RemoveGestureRecognizer(touchRecognizer); #endif