在Mountain Lion中使用IKImageView和NSScrollView进行双指拖动

时间:2012-09-01 21:31:34

标签: macos cocoa osx-mountain-lion nsscrollview ikimageview

我有一个Mac应用程序已经在应用程序商店中存在了一年左右。它最初是与目标SDK 10.7,Lion一起发布的。在更新Mountain Lion后,它不再有效。

应用程序在嵌入在NSScrollView中的IKImageView中显示大图像。将其放入滚动视图的目的是让两个手指拖动工作,而不是用户必须单击拖动。使用Nicholas Riley的ScrollViewWorkaround,我可以使用两个手指滚动来显示用户放大后剪辑的内容。就像您在预览应用中看到的一样。

Nicholas Riley的解决方案: IKImageView and scroll bars

现在在Mountain Lion中这不起作用。放大,缩放或缩放按钮后,图像被锁定在图像的左下部分。它不会滚动。

所以问题是,在IKImageView中显示大图像的适当方法是什么,并且有两个手指拖动缩放图像?

谢谢你,
有状态

1 个答案:

答案 0 :(得分:9)

嗯,Nicholas Riley的解决方案是一个丑陋的黑客,因为它解决了错误的阶级;问题不在于 NSClipView (他将其子类化,但效果不错),但使用 IKImageView

IKImageView 的问题实际上非常简单(天知道为什么Apple没有修复这个问题?... 7年......):它的大小不适应大小它显示的图像。现在,当您在 NSScrollView 中嵌入 IKImageView 时,滚动视图显然只能相对于嵌入的 IKImageView 的大小调整其滚动条,而不是它包含的图像。由于 IKImageView 的大小始终保持不变,因此滚动条不会按预期工作。

以下代码子类 IKImageView 并修复了此行为。唉,一旦你缩放,它就不会解决 IKImageView 在Mountain Lion中容易崩溃的事实......

///////////////////// HEADER FILE - FixedIKImageView.h

#import <Quartz/Quartz.h>

@interface FixedIKImageView : IKImageView
@end






///////////////////// IMPLEMENTATION FILE - FixedIKImageView.m

#import "FixedIKImageView.h"


@implementation FixedIKImageView

- (void)awakeFromNib
    {
        [self setTranslatesAutoresizingMaskIntoConstraints:NO]; // compatibility with Auto Layout; without this, there could be Auto Layout error messages when we are resized (delete this line if your app does not use Auto Layout)
    }


// FixedIKImageView must *only* be used embedded within an NSScrollView. This means that setFrame: should never be called explicitly from outside the scroll view. Instead, this method is overwritten here to provide the correct behavior within a scroll view. The new implementation ignores the frameRect parameter.
- (void)setFrame:(NSRect)frameRect
    {
        NSSize  imageSize = [self imageSize];
        CGFloat zoomFactor = [self zoomFactor];
        NSSize  clipViewSize = [[self superview] frame].size;

        // The content of our scroll view (which is ourselves) should stay at least as large as the scroll clip view, so we make ourselves as large as the clip view in case our (zoomed) image is smaller. However, if our image is larger than the clip view, we make ourselves as large as the image, to make the scrollbars appear and scale appropriately.
        CGFloat newWidth = (imageSize.width * zoomFactor < clipViewSize.width)?  clipViewSize.width : imageSize.width * zoomFactor;
        CGFloat newHeight = (imageSize.height * zoomFactor < clipViewSize.height)?  clipViewSize.height : imageSize.height * zoomFactor;

        [super setFrame:NSMakeRect(0, 0, newWidth - 2, newHeight - 2)]; // actually, the clip view is 1 pixel larger than the content view on each side, so we must take that into account
    }


//// We forward size affecting messages to our superclass, but add [self setFrame:NSZeroRect] to update the scroll bars. We also add [self setAutoresizes:NO]. Since IKImageView, instead of using [self setAutoresizes:NO], seems to set the autoresizes instance variable to NO directly, the scrollers would not be activated again without invoking [self setAutoresizes:NO] ourselves when these methods are invoked.

- (void)setZoomFactor:(CGFloat)zoomFactor
    {
        [super setZoomFactor:zoomFactor];
        [self setFrame:NSZeroRect];
        [self setAutoresizes:NO];
    }


- (void)zoomImageToRect:(NSRect)rect
    {
        [super zoomImageToRect:rect];
        [self setFrame:NSZeroRect];
        [self setAutoresizes:NO];
    }


- (void)zoomIn:(id)sender
    {
        [super zoomIn:self];
        [self setFrame:NSZeroRect];
        [self setAutoresizes:NO];
    }


- (void)zoomOut:(id)sender
    {
        [super zoomOut:self];
        [self setFrame:NSZeroRect];
        [self setAutoresizes:NO];
    }


- (void)zoomImageToActualSize:(id)sender
    {
        [super zoomImageToActualSize:sender];
        [self setFrame:NSZeroRect];
        [self setAutoresizes:NO];
    }


- (void)zoomImageToFit:(id)sender
    {
        [self setAutoresizes:YES];  // instead of invoking super's zoomImageToFit: method, which has problems of its own, we invoke setAutoresizes:YES, which does the same thing, but also makes sure the image stays zoomed to fit even if the scroll view is resized, which is the most intuitive behavior, anyway. Since there are no scroll bars in autoresize mode, we need not add [self setFrame:NSZeroRect].
    }


- (void)setAutoresizes:(BOOL)autoresizes    // As long as we autoresize, make sure that no scrollers flicker up occasionally during live update.
    {
        [self setHasHorizontalScroller:!autoresizes];
        [self setHasVerticalScroller:!autoresizes];
        [super setAutoresizes:autoresizes];
    }


@end