Exemplo n.º 1
0
        protected virtual void TouchGestureRecognizerOnPinch(object sender, PinchEventArgs args)
        {
            var previousPoint = _skScene.GetCanvasPointFromViewPoint(args.PreviousPoint);
            var newPoint      = _skScene.GetCanvasPointFromViewPoint(args.NewPoint);
            var pivotPoint    = _skScene.GetCanvasPointFromViewPoint(args.PivotPoint);

            SKPoint oldVector = previousPoint - pivotPoint;
            SKPoint newVector = newPoint - pivotPoint;

            if (TouchManipulationMode == TouchManipulationMode.ScaleRotate)
            {
                // Find angles from pivot point to touch points
                float oldAngle = (float)Math.Atan2(oldVector.Y, oldVector.X);
                float newAngle = (float)Math.Atan2(newVector.Y, newVector.X);

                // Calculate rotation matrix
                float angle = newAngle - oldAngle;
                _skScene.RotateByRadiansDelta(pivotPoint, angle);

                // Effectively rotate the old vector
                float magnitudeRatio = oldVector.GetMagnitude() / newVector.GetMagnitude();
                oldVector.X = magnitudeRatio * newVector.X;
                oldVector.Y = magnitudeRatio * newVector.Y;
            }

            var scale = newVector.GetMagnitude() / oldVector.GetMagnitude();

            if (!float.IsNaN(scale) && !float.IsInfinity(scale))
            {
                _skScene.ZoomByScaleFactor(pivotPoint, scale);
            }
        }
        protected virtual void TouchGestureRecognizerOnPinch(object sender, PinchEventArgs args)
        {
            if (args.TouchActionType != TouchActionType.Moved)
            {
                return;
            }

            var previousPoint         = args.PreviousPoint;
            var newPoint              = args.NewPoint;
            var pivotPoint            = args.PivotPoint;
            var transformedPivotPoint = _skScene.GetCanvasPointFromViewPoint(pivotPoint);

            SKPoint oldVector = previousPoint - pivotPoint;
            SKPoint newVector = newPoint - pivotPoint;

            if (TouchManipulationMode == TouchManipulationMode.ScaleRotate)
            {
                float angle = GetAngleBetweenVectors(oldVector, newVector);

                _skScene.RotateByRadiansDelta(transformedPivotPoint, angle);

                // Effectively rotate the old vector
                float magnitudeRatio = oldVector.GetMagnitude() / newVector.GetMagnitude();
                oldVector.X = magnitudeRatio * newVector.X;
                oldVector.Y = magnitudeRatio * newVector.Y;
            }
            else if (TouchManipulationMode == TouchManipulationMode.IsotropicScale &&
                     EnableTwoFingersPanInIsotropicScaleMode)
            {
                float angle = GetAngleBetweenVectors(oldVector, newVector);

                // Calculate the movement as a distance between old vector and a new vector
                // but in orthogonal direction to the old vector.

                float   oldVectorOriginPoint = newVector.GetMagnitude() * (float)Math.Cos(angle);
                float   magnitudeRatio       = oldVectorOriginPoint / oldVector.GetMagnitude();
                SKPoint oldVectorOrigin      = new SKPoint(oldVector.X * magnitudeRatio, oldVector.Y * magnitudeRatio);
                SKPoint moveVector           = newVector - oldVectorOrigin;
                SKPoint dividedMoveVector    = new SKPoint(moveVector.X * 0.5f, moveVector.Y * 0.5f);

                _skScene.MoveByVector(dividedMoveVector);
            }

            var scale = newVector.GetMagnitude() / oldVector.GetMagnitude();

            if (!float.IsNaN(scale) && !float.IsInfinity(scale))
            {
                _skScene.ZoomByScaleFactor(transformedPivotPoint, scale);
            }
        }
 protected override void TouchGestureRecognizerOnPinch(object sender, PinchEventArgs args)
 {
     base.TouchGestureRecognizerOnPinch(sender, args);
     ProcessGestureDelays(ref _lastPinchTime, _minGestureDuration, args.TouchActionType);
 }