0

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

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

Nicholas Riley 的解决方案: IKImageView 和滚动条

现在在山狮这行不通。放大、捏合或缩放按钮后,图像被锁定在图像的左下方。它不会滚动。

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

谢谢,
有状态的

4

1 回答 1

9

好吧,Nicholas Riley 的解决方案是一个丑陋的 hack,因为它解决了错误的类;问题不在于 NSClipView(他对其进行了子类化,但按原样工作得很好),而在于 IKImageView

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

以下代码子类化了 IKImageView并修复了此行为。唉,它无法解决IKImageView在 Mountain Lion 中一旦缩放就容易崩溃的事实……</p>

///////////////////// 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
于 2012-09-25T00:31:02.087 回答