public override void TouchesBegan(NSSet touches, UIEvent evt) { base.TouchesBegan(touches, evt); try { UITouch[] arr = evt.AllTouches.ToArray <UITouch>(); if (arr.Length == 2) { _resize = Math.Sqrt(Math.Abs(arr[0].LocationInView(this).X - arr[1].LocationInView(this).X) * Math.Abs(arr[0].LocationInView(this).X - arr[1].LocationInView(this).X) + Math.Abs(arr[0].LocationInView(this).Y - arr[1].LocationInView(this).Y) * Math.Abs(arr[0].LocationInView(this).Y - arr[1].LocationInView(this).Y)); // _resizeReady = true; ignore_action = false; } UITouch touch = touches.AnyObject as UITouch; if (touch != null) { _touchesX = touch.LocationInView(this).X; _touchesY = touch.LocationInView(this).Y; GestureFrame _gi = (GestureFrame)this.Element; EventArgs e = new EventArgs(); _gi.OnDown(this, e, (float)_touchesX, (float)_touchesY, (int)touch.TapCount); } } catch (Exception ex) { } }
protected override void OnElementChanged(ElementChangedEventArgs <Frame> e) { base.OnElementChanged(e); GestureFrame control = (GestureFrame)Element; if (control == null) { return; } if (control.IsSwipe) { taped = new UITapGestureRecognizer( (action) => { try { if (action.State == UIGestureRecognizerState.Ended) { CGPoint point = action.LocationOfTouch(0, null); GestureFrame _gi = (GestureFrame)this.Element; float scale = (float)UIScreen.MainScreen.Scale; _gi.OnUp(this, e, ((float)point.X) * scale, ((float)point.Y) * scale); _gi.OnTapped(this, e, ((float)point.X) * scale, ((float)point.Y) * scale); } else if (action.State == UIGestureRecognizerState.Began) { CGPoint point = action.LocationOfTouch(0, null); GestureFrame _gi = (GestureFrame)this.Element; float scale = (float)UIScreen.MainScreen.Scale; _gi.OnDown(this, e, ((float)point.X) * scale, ((float)point.Y) * scale, 0); } else if (action.State == UIGestureRecognizerState.Changed) { CGPoint point = action.LocationOfTouch(0, null); GestureFrame _gi = (GestureFrame)this.Element; float scale = (float)UIScreen.MainScreen.Scale; _gi.OnMove(this, e, ((float)point.X) * scale, ((float)point.Y) * scale); } } catch (Exception ex) { Logs.LogEx(ex); } } ) { }; swipeDown = new UISwipeGestureRecognizer( () => { GestureFrame _gi = (GestureFrame)this.Element; _gi.OnSwipeDown(); } ) { Direction = UISwipeGestureRecognizerDirection.Down, }; swipeUp = new UISwipeGestureRecognizer( () => { GestureFrame _gi = (GestureFrame)this.Element; _gi.OnSwipeTop(); } ) { Direction = UISwipeGestureRecognizerDirection.Up, }; swipeLeft = new UISwipeGestureRecognizer( () => { GestureFrame _gi = (GestureFrame)this.Element; _gi.OnSwipeLeft(); } ) { Direction = UISwipeGestureRecognizerDirection.Left, }; swipeRight = new UISwipeGestureRecognizer( () => { GestureFrame _gi = (GestureFrame)this.Element; _gi.OnSwipeRight(); } ) { Direction = UISwipeGestureRecognizerDirection.Right, }; if (e.NewElement == null) { if (swipeDown != null) { this.RemoveGestureRecognizer(swipeDown); } if (swipeUp != null) { this.RemoveGestureRecognizer(swipeUp); } if (swipeLeft != null) { this.RemoveGestureRecognizer(swipeLeft); } if (swipeRight != null) { this.RemoveGestureRecognizer(swipeRight); } if (taped != null) { this.RemoveGestureRecognizer(taped); } } if (e.OldElement == null) { this.AddGestureRecognizer(swipeDown); this.AddGestureRecognizer(swipeUp); this.AddGestureRecognizer(swipeLeft); this.AddGestureRecognizer(swipeRight); this.AddGestureRecognizer(taped); } } }
void HandleTouch(object sender, TouchEventArgs e) { bool cancelBase = false; if (e.Event.ActionMasked == MotionEventActions.Down) { _tapedDownTime = e.Event.EventTime; _tapedX = e.Event.RawX; _tapedY = e.Event.RawY; GestureFrame _gi = (GestureFrame)this.Element; _gi.OnDown(sender, e, e.Event.RawX, e.Event.RawY, e.Event.ActionIndex); } if (e.Event.ActionMasked == MotionEventActions.Pointer1Up) { GestureFrame _gi = (GestureFrame)this.Element; _gi.OnResizeDone(sender, e); _resizeReady = false; ignore_action = true; } if (e.Event.ActionMasked == MotionEventActions.Pointer1Down) { _resizeReady = true; ignore_action = false; if (_resizeReady) { MotionEvent.PointerCoords p0 = new MotionEvent.PointerCoords(); MotionEvent.PointerCoords p1 = new MotionEvent.PointerCoords(); e.Event.GetPointerCoords(0, p0); e.Event.GetPointerCoords(1, p1); _resize = Math.Sqrt(Math.Abs(p0.X - p1.X) * Math.Abs(p0.X - p1.X) + Math.Abs(p0.Y - p1.Y) * Math.Abs(p0.Y - p1.Y)); } } else if (e.Event.ActionMasked == MotionEventActions.Up) { GestureFrame _gi = (GestureFrame)this.Element; _gi.OnUp(sender, e, e.Event.RawX, e.Event.RawY); if ((e.Event.EventTime - _tapedDownTime < 250) && Math.Abs(_tapedX - e.Event.RawX) < 20 && Math.Abs(_tapedY - e.Event.RawY) < 20) { _gi.OnTapped(sender, e, e.Event.RawX, e.Event.RawY); } if (e.Event.PointerCount <= 1) { ignore_action = false; } } else if (e.Event.ActionMasked == MotionEventActions.Move) { GestureFrame _gi = (GestureFrame)this.Element; double rs = DetectResizeMotion(e); if (-1 != rs) { _gi.OnResize(sender, e, _resize, rs); } else if (!ignore_action) { float d = Forms.Context.ApplicationContext.Resources.DisplayMetrics.Density; float deltaX = _tapedX - e.Event.RawX; float deltaY = _tapedY - e.Event.RawY; cancelBase = _gi.OnMove(sender, e, deltaX / d, deltaY / d); } } _detector.OnTouchEvent(e.Event); }