How to interpret trackpad pinch gestures to zoom IKImageBrowserView
Asked Answered
A

2

8

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?

Annoy answered 27/11, 2009 at 9:12 Comment(0)
E
15

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

Earful answered 27/11, 2009 at 23:46 Comment(0)
S
5

If you add the event magnification to your zoom factor, the same event will not have the same effect depending on the current zoom factor. Adding a magnification of 0.1 to a zoom factor of 1 will zoom by 10%, but if the zoom factor is 0.1, adding 0.1 will double your zoom.

The result of multiplying the zoom factor by magnification + 1.0 will be more consistent.

I prefer multiplying the zoom factor by std::exp(magnification), as it seems to be a more natural solution. A magnification of -n will zoom out exactly by the same amount that a magnification of n zooms in.

Selfpossessed answered 4/5, 2010 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.