objective-c - NSScrollView 不滚动

标签 objective-c cocoa osx-lion appkit nsscrollview

我正在移植一个使用此 tutorial 的 iPhone 应用程序进入 Mac 应用程序。我已经能够在 NSScrollView 中显示文本,但无法让文本滚动,我希望有人知道我缺少什么。

在 iPhone 应用程序中,CTView 是 UIScrollView 的子类

CTView.h:

@interface CTView : UIScrollView <UIScrollViewDelegate> {

1)但是AppKit中没有NSScrollViewDelegate。 NSScrollView 缺少委托(delegate)接口(interface)是问题所在吗?

因此,对于 Mac 应用程序,CTView.h 如下所示:

@interface CTView : NSScrollView 

在 CTView.m 中我有

[self setHasHorizontalScroller:YES];
[self setAcceptsTouchEvents:YES];

还在 CTView.m 中,我创建了一个 NSMutableArray 帧作为

self.frames = [NSMutableArray array];

并用框架填充数组。我还为完整的较大图像设置了文档 View 的框架,我希望能够滚动该图像:

[[self contentView] setFrame:[self bounds]];
[self.documentView setFrame:  NSMakeRect(0, 0, totalPages * self.bounds.size.width, textFrame.size.height)];

第一帧在 CTView 中可见,但是当我移动水平滚动条时,第二帧在 CTView 中不显示。

我使用的是 Xcode 4.3.3 和 Mac OS X 10.7.4。

有谁知道我需要做什么与 UIScrollView 不同才能使 NSScrollView 能够滚动框架?

最佳答案

终于明白了。哇!

1) NSScrollView 需要一个用于其 documentView 的 View ,其中 documentView 是滚动的内容。因此,在 Nib 中,我创建了一个 NSScrollView,其中包含 documentView 的 View ,并将 documentView 设置为该 View ,如 CTView.m 中的代码所示。

NSArray *viewArray     = [self subviews];
NSArray *docViewArray  = [[viewArray objectAtIndex:0] subviews]; 
NSView  *docView       = [docViewArray objectAtIndex:0];

[self setDocumentView:docView];

2) 然后我将通常添加到 UIScrollView 的框架数组添加到 NSScrollView 的 documentView 中。框架数组由多个内容元素组成。

 while (textPos < [attString length]) { 
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), innerPath, NULL);
    CFRange frameRange = CTFrameGetVisibleStringRange(frame); 

   CTColumnView* content = [[[CTColumnView alloc] initWithFrame: NSMakeRect(0, 0, self.contentSize.width, self.contentSize.height)] autorelease];
    [docView addSubview: content];  // <<-- Here is where the frames are added to the documentView
    textPos += frameRange.length;
  }

这样就成功了。

关于objective-c - NSScrollView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11318562/

相关文章:

ios - UIAlertView 延迟或不显示

mysql - 在 Objective-C 中处理 MySQL 中的特殊字母

macos - 响应式滚动 - setWantsLayer Webkit 问题

ios - NSNotification 用户信息示例?

osx-snow-leopard - 适用于 MacOSX 10.7、10.6 和 10.5 的单个应用程序

cocoa - Mac OS X Lion 全屏事件

ruby-on-rails - 不同rvm gemset中的rake版本

iphone - 如何从 UIWebview 或 dealloc UIWebview 中删除缓存

cocoa - SIGPIPE 异常

ios - 如何在 UITextview 中使用不同的字体样式但相同的字体系列呈现 HTML 字符串