views:

352

answers:

2

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?

+2  A: 

Hi.

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

Matthias Gansrigler
Works perfectly. Thank you.
Fraser Speirs
A: 

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.

Thomas