void renderer_Touch(object sender, global::Android.Views.View.TouchEventArgs e) { VisualElementRenderer <global::Xamarin.Forms.View> renderer = sender as VisualElementRenderer <global::Xamarin.Forms.View>; Element element = renderer.Element; HandleTouch(element, e.Event); e.Handled = true; }
/// <summary> /// Handles the touch. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="Android.Views.View.TouchEventArgs"/> instance containing the event data.</param> void HandleTouch(object sender, global::Android.Views.View.TouchEventArgs e) { var element = this.Element as ExtendedEntry; switch (e.Event.Action) { case MotionEventActions.Down: _downX = e.Event.GetX(); _downY = e.Event.GetY(); return; case MotionEventActions.Up: case MotionEventActions.Cancel: case MotionEventActions.Move: _upX = e.Event.GetX(); _upY = e.Event.GetY(); float deltaX = _downX - _upX; float deltaY = _downY - _upY; // swipe horizontal? if (Math.Abs(deltaX) > Math.Abs(deltaY)) { if (Math.Abs(deltaX) > MIN_DISTANCE) { // left or right if (deltaX < 0) { element.OnRightSwipe(this, EventArgs.Empty); return; } if (deltaX > 0) { element.OnLeftSwipe(this, EventArgs.Empty); return; } } else { global::Android.Util.Log.Info("ExtendedEntry", "Horizontal Swipe was only " + Math.Abs(deltaX) + " long, need at least " + MIN_DISTANCE); return; // We don't consume the event } } // swipe vertical? // else // { // if(Math.abs(deltaY) > MIN_DISTANCE){ // // top or down // if(deltaY < 0) { this.onDownSwipe(); return true; } // if(deltaY > 0) { this.onUpSwipe(); return true; } // } // else { // Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE); // return false; // We don't consume the event // } // } return; } }
/* * bool ShareMotionEvent(Element element, MotionEvent e, VisualElement eventElement) { * if (element.Parent != null) { * foreach (Listener listener in Listener.Listeners) { * if (listener.Element == element.Parent) { * NativeGestureHandler parentNativeGestureHandler = GetInstanceForElement (listener.Element); * * var parentEvent = MotionEvent.Obtain (e); * Point offset = VisualElementExtensions.CoordTransform (eventElement, Point.Zero, listener.Element); * var scale = Forms.Context.Resources.DisplayMetrics.Density; * parentEvent.OffsetLocation ((float)(offset.X * scale), (float)(offset.Y * scale)); * if (_debugEvents) System.Diagnostics.Debug.WriteLine ("\t parentEvent=["+parentEvent+"]"); * return parentNativeGestureHandler.HandleMotionEvent (parentEvent); * } * } * return ShareMotionEvent (element.Parent, e, eventElement); * } else { * return false; * } * } */ void HandleTouch(object sender, global::Android.Views.View.TouchEventArgs e) { //System.Diagnostics.Debug.WriteLine("NativeGestureHandler.HandlTouch"); var handled = HandleMotionEvent(e.Event); if (e.Event.Action == MotionEventActions.Down) { e.Handled = true; XamarinForms_2_4_WorkAround(); } }
private void OnTouch(object sender, global::Android.Views.View.TouchEventArgs args) { TouchActionType?type = null; if (args.Event.Action == MotionEventActions.Down) { _isPressed = true; type = TouchActionType.Pressed; } else if (args.Event.Action == MotionEventActions.Up) { _isPressed = false; type = TouchActionType.Released; } else if (args.Event.Action == MotionEventActions.Cancel) { _isPressed = false; type = TouchActionType.Cancelled; } else if (args.Event.Action == MotionEventActions.Outside) { type = TouchActionType.Cancelled; } else if (args.Event.Action == MotionEventActions.Move) { type = TouchActionType.Move; } else if (args.Event.Action == MotionEventActions.Move && (args.Event.GetX() < 0 || args.Event.GetY() < 0 || args.Event.GetX() > _view.Width || args.Event.GetY() > _view.Height)) { type = TouchActionType.Cancelled; } if (_touchEffect != null && type.HasValue) { Point point = PxToDp(new Point(args.Event.GetX(), args.Event.GetY())); Point applicationPoint = PxToDp(new Point(args.Event.RawX, args.Event.RawY)); _touchEffect.OnTouchAction(Element, new TouchActionEventArgs(type.Value, point, applicationPoint, _isPressed)); } }
void OnTouch(object sender, global::Android.Views.View.TouchEventArgs args) { // Two object common to all the events global::Android.Views.View senderView = sender as global::Android.Views.View; MotionEvent motionEvent = args.Event; // Get the pointer index int pointerIndex = motionEvent.ActionIndex; // Get the id that identifies a finger over the course of its progress int id = motionEvent.GetPointerId(pointerIndex); senderView.GetLocationOnScreen(twoIntArray); Point screenPointerCoords = new Point(twoIntArray[0] + motionEvent.GetX(pointerIndex), twoIntArray[1] + motionEvent.GetY(pointerIndex)); // Use ActionMasked here rather than Action to reduce the number of possibilities switch (args.Event.ActionMasked) { case MotionEventActions.Down: case MotionEventActions.PointerDown: FireEvent(this, id, TouchActionType.Pressed, screenPointerCoords, true); idToEffectDictionary.Add(id, this); capture = libTouchEffect.Capture; break; case MotionEventActions.Move: // Multiple Move events are bundled, so handle them in a loop for (pointerIndex = 0; pointerIndex < motionEvent.PointerCount; pointerIndex++) { id = motionEvent.GetPointerId(pointerIndex); if (capture) { senderView.GetLocationOnScreen(twoIntArray); screenPointerCoords = new Point(twoIntArray[0] + motionEvent.GetX(pointerIndex), twoIntArray[1] + motionEvent.GetY(pointerIndex)); FireEvent(this, id, TouchActionType.Moved, screenPointerCoords, true); } else { CheckForBoundaryHop(id, screenPointerCoords); if (idToEffectDictionary[id] != null) { FireEvent(idToEffectDictionary[id], id, TouchActionType.Moved, screenPointerCoords, true); } } } break; case MotionEventActions.Up: case MotionEventActions.Pointer1Up: if (capture) { FireEvent(this, id, TouchActionType.Released, screenPointerCoords, false); } else { CheckForBoundaryHop(id, screenPointerCoords); if (idToEffectDictionary[id] != null) { FireEvent(idToEffectDictionary[id], id, TouchActionType.Released, screenPointerCoords, false); } } idToEffectDictionary.Remove(id); break; case MotionEventActions.Cancel: if (capture) { FireEvent(this, id, TouchActionType.Cancelled, screenPointerCoords, false); } else { if (idToEffectDictionary[id] != null) { FireEvent(idToEffectDictionary[id], id, TouchActionType.Cancelled, screenPointerCoords, false); } } idToEffectDictionary.Remove(id); break; } }