如何将滚动输入传递到不同的视图

时间:2011-04-01 00:40:11

标签: objective-c cocoa nsscrollview

这个网站真的很棒。

这次我希望这是一个简单的问题。我想将用户的任何滚动输入(可能是滚轮,触摸板等)传递给包含我自己的子视图的NSScrollView。

如果用户仅在documentView上滚动(在我的子视图的框架之外),滚动工作正常,但如果他们在光标在子视图上滚动时没有任何反应。所以基本上我想让子视图识别滚动事件并将其传递回滚动视图。

非常感谢任何帮助。

干杯。

编辑: 这是我用来将子视图添加到documentView的代码 _milestoneView和_activityView都是NSView子类,它们具有相应的nib(使用instantiateNibWithOwner创建并且相应地连接了对象)它们包含NSTextField,PXListView,并且一些具有NSProgressIndicator。

-(void)useProject:(NSNumber *)projectId
{
[self resetView];

NSRect bounds = [[self view] bounds];
NSRect defaultFrame = NSMakeRect(20, NSMaxY(bounds)-93, NSMaxX(bounds)-20, 50);


//Prepare the milestone view
if (_milestoneView == nil)
    _milestoneView = [MilestoneView milestoneViewFromNibWithFrame:defaultFrame withProject:[BCLocalFetch projectForId:projectId]];
[_milestoneView reloadList];


//Prepare the activity view
if (_activityView == nil)
    _activityView = [ActivityView activityViewFromNibWithFrame:defaultFrame withProject:[BCLocalFetch projectForId:projectId]];

[self refresh];
}

然后我使用刷新方法重新定位它们,因为内容大小不同,所以我想要一个单独的方法。

-(void)refresh
{
//Position the milestones first
[_milestoneView setFrameOrigin:NSMakePoint(20, NSMaxY([[self view] bounds])-[_milestoneView frame].size.height-60)];
if ([[_milestoneView milestones] count] > 0)
    [[self view] addSubview:_milestoneView];

//Now the activity view
[_activityView setFrameOrigin:NSMakePoint(20, [_milestoneView frame].origin.y-[_activityView frame].size.height-20)];
[[self view] addSubview:_activityView];

[self autosizeView];
}

-(void)autosizeView
{
//Resize the view to accommodate all subviews
NSRect oldFrame = [[self view] frame];
CGFloat lastY = [_activityView frame].origin.y;
if (lastY < 0) {
    CGFloat newHeight = oldFrame.size.height + (-lastY);
    [[self view] setFrameSize:NSMakeSize(oldFrame.size.width, newHeight)];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"BBContentDidResizeNotification" object:self];
}

1 个答案:

答案 0 :(得分:0)

好的,所以我回到了这个问题,最后得到了解决方案。实施实际上非常简单。 我向PXListView添加了一个属性,指向要滚动的NSScrollView。 然后我实现了NSResponder的scrollWheel:这样的方法:

-(void)scrollWheel:(NSEvent *)theEvent
{
    //Pass scrolling to the superview
    [_scrollHandler scrollWheel:theEvent];
}

一切都很好!

相关问题