cocoamacosimagekitikimagebrowserview

How to interpret trackpad pinch gestures to zoom IKImageBrowserView


I have an IKImageBrowserView that I want to be able to pinch-zoom using a multi-touch trackpad on a recent Mac laptop.

The Cocoa Event Handling Guide, in the section Handling Gesture Events says:

The magnification accessor method returns a floating-point (CGFloat) value representing a factor of magnification

..and goes on to show code that adjusts the size of the view by multiplying height and width by magnification + 1.0.

This doesn't seem to be the right approach for zooming IKImageBrowserView, whose zoomValue property is clamped between 0.0 and 1.0.

So, does anyone know how to interpret the event in -[NSResponder magnifyWithEvent:] to zoom IKImageBrowserView?


Solution

  • This is what I do, on Snow Leopard, it runs perfectly well:

    In 10.6, NSEvent has the method "magnification" which will return the right amount already. All you have to do is add this to the old value, like [imageBrowser zoomValue]+[event magnification].

    - (void)magnifyWithEvent:(NSEvent *)event
    {
        if ([event magnification] > 0)
        {
            if ([self zoomValue] < 1)
            {
                [self setZoomValue:[self zoomValue] + [event magnification]];
            }
        }
        else if ([event magnification] < 0)
        {
            if ([self zoomValue] + [event magnification] > 0.45)
            {
                [self setZoomValue:[self zoomValue] + [event magnification]];
            }
            else
            {
                [self setZoomValue:0.45];
            }
        }
    }
    

    self here is a IKImageBrowserView subclass. I have a threshold here so that the zoomValue can not get smaller than 0.45, but that's just how I like it.

    Best regards, Matthias, Eternal Storms Software