views:

250

answers:

2

What's the best practice for setting zoom factor of an image within IKImageView via NSSlider?

I was able to bind a slider either to zoom in OR zoom out action of an IKImageView. Obviously, what I'd rather see is a single slider controlling both those actions. Best, if image is refreshed after each change of the slider (continuously, even if a mouse button is not released yet).

A: 

This demo explains a lot: ImageKitDemo

In particular, this snippet is what I was looking for:

- (IBAction) zoomSliderDidChange:(id)sender
{
    [addProductPhotoImageView setZoomFactor:[sender floatValue]];
}
piobyz
You'll want to set the `minValue` and `maxValue` of the slider first, most probably in IB.
Peter Hosey
That's right, as well as continuous state.
piobyz
+1  A: 

The Bindings way would be to bind both the Zoom Factor of the IK image view and the Value of the slider to the same property of your controller. When the slider changes the value of the property, the image view will be notified, and will go get the new value from your controller.

One advantage of this way is that you can add more ways of zooming in and out and the value in the slider won't go stale. For one example, if IKImageView adds pinch-zooming (or if it has it already—I don't have multi-touch on my Mac), the user can zoom that way and the slider will update automatically. That won't happen with the IBAction solution.

Another example would be Zoom In and Zoom Out menu commands (perhaps with ⌘+ and ⌘- keyboard shortcuts) that send action messages to your controller. Your controller would respond by increasing or decreasing the value of the property (using a setter method it implements). With Bindings, both the image view and the slider will update for free. Without Bindings, you would have to explicitly talk to both the image view and the slider, telling one to update its zoom factor and the other to update its slider.

A third example would be a “Zoom factor: X%” display in a corner of your window. With Bindings, this can update for free no matter how the user zooms the image: moving the slider thumb, pinching/unpinching the image, or pressing a menu item. Without Bindings, this would be yet another thing you have to talk to in your (at least three) change-the-zoom-value action methods.

Peter Hosey