public override async void ViewDidLoad() { base.ViewDidLoad(); CanvasView.PaintSurface += CanvasView_PaintSurface; var pan = new NSPanGestureRecognizer(MapPanned); var zoom = new NSMagnificationGestureRecognizer(MapZoomed); _screenScale = (float)NSScreen.MainScreen.BackingScaleFactor; _canvasSize = new SKSize(20000, 2000); CanvasView.AddGestureRecognizer(zoom); CanvasView.AddGestureRecognizer(pan); //load images for (int i = 1; i < 16; i++) { var img = await ImageService.Instance.LoadFileFromApplicationBundle("img/img" + i + ".jpg").AsNSImageAsync(); var sizeRandom = rand.NextDouble() * (1.2 - 0.2) + 0.2; var size = new SKSize((float)(img.Size.Width * sizeRandom), (float)(img.Size.Height * sizeRandom)); var rect = SKRect.Empty; rect.Size = size; var pointRandomW = rand.NextDouble() * (_canvasSize.Width - 0) + 0; var pointRandomH = rand.NextDouble() * (_canvasSize.Height - 0) + 0; rect.Location = new SKPoint((float)pointRandomW, (float)pointRandomH); rects.Add(rect); imgs.Add(img.ToSKImage()); } }
void MapZoomed(NSMagnificationGestureRecognizer obj) { var p = obj.LocationInView(CanvasView); var mouseLocation = NSEvent.CurrentMouseLocation; var pp = new SKPoint((float)(_canvasSize.Width * p.X / CanvasView.Bounds.Width), (float)(_canvasSize.Height - (_canvasSize.Height * p.Y / CanvasView.Bounds.Height))); pp = _im.MapPoint(pp); var mouseCursorX = (float)p.X; var mouseCursorY = (float)(CanvasView.Frame.Height - p.Y); if (obj.State == NSGestureRecognizerState.Began) { StartPanZoom(new SKPoint(mouseCursorX, mouseCursorY)); } else if (obj.State == NSGestureRecognizerState.Changed) { var x = obj.Magnification + 1; DoPanZoom(_startM, _startAnchorPt, _totalDistance, x); } else if (obj.State == NSGestureRecognizerState.Cancelled) { _isPanZoom = false; _m.TryInvert(out _im); } else if (obj.State == NSGestureRecognizerState.Ended) { _isPanZoom = false; _m.TryInvert(out _im); } }
/// <summary> /// Initializes a new instance of the <see cref="PinchRecognizer"/> class. /// </summary> /// <param name="view">The native view.</param> public PinchRecognizer(NSView view) : base(view) { _recognizer = new NSMagnificationGestureRecognizer(OnPinching); view.AddGestureRecognizer(_recognizer); void OnPinching(NSMagnificationGestureRecognizer recognizer) { var point = recognizer.LocationInView(View); switch (recognizer.State) { case NSGestureRecognizerState.Began: OnTouchesBegan(point.X, point.Y); OnPinchingBegan(point.X, point.Y); break; case NSGestureRecognizerState.Changed: OnPinchingScaleChanged(recognizer.Magnification); break; case NSGestureRecognizerState.Cancelled: OnPinchingStateChanged(GestureState.Cancelled); OnTouchesEnded(point.X, point.Y); break; case NSGestureRecognizerState.Failed: OnPinchingStateChanged(GestureState.Failed); OnTouchesEnded(point.X, point.Y); break; case NSGestureRecognizerState.Ended: OnPinchingStateChanged(GestureState.Ended); OnTouchesEnded(point.X, point.Y); break; } } }
NSMagnificationGestureRecognizer CreatePinchRecognizer(Action <NSMagnificationGestureRecognizer> action) { var result = new NSMagnificationGestureRecognizer(action); return(result); }