7

我正在尝试创建一个类似 Reeder/Sparrow 的 UI 来处理我的应用程序的内容。目前我使用一个带有两个 NSView 的 NSSplitView(左边的一个是内容列表,另一个是“检查器”)。

我想知道的是如何在标题栏上创建分隔线,它也可以作为拆分视图的分隔线。我已经在使用INAppStoreWindow子类了。

有任何想法吗?提前感谢

4

1 回答 1

8

我这样做的方法是添加一个 NSSplitView 子类作为 INAppStoreWindow 的 tileBarView 的子视图:

// This code comes from the INAppStoreWindow readme
INAppStoreWindow *appStoreWindow = (INAppStoreWindow *)[self window];

// self.titleView is a an IBOutlet to an NSView that has been configured in IB with everything you want in the title bar
self.windowTitleBarView.frame = appStoreWindow.titleBarView.bounds;
self.windowTitleBarView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[appStoreWindow.titleBarView addSubview:self.windowTitleBarView];

两个棘手的部分是让这个拆分视图表现得像一个标题栏(即仍然允许您拖动窗口),并将标题栏中的拆分视图与窗口中的主拆分视图同步,所以它们看起来像对用户来说同样的事情。

要解决第一个问题,您必须做的不仅仅是-mouseDownCanMovewWindow在标题栏 NSSplitView 子类中返回 YES。如果您这样做,则磁贴栏的任何子视图都不会响应鼠标事件。相反,请执行以下操作:

@implementation MyTitleBarSplitView

- (BOOL)mouseDownCanMoveWindow
{
    return NO;
}

// Code below adapted from http://www.cocoabuilder.com/archive/cocoa/219261-conditional-mousedowncanmovewindow-for-nsview.html
- (void)mouseDown:(NSEvent*)theEvent 
{
    NSWindow *window = [self window];
    NSPoint mouseLocation = [theEvent locationInWindow];
    NSRect dividerRect = NSMakeRect(NSMaxX([[[self subviews] objectAtIndex:0] frame]), 
                                    NSMinY([self bounds]), 
                                    [self dividerThickness], 
                                    NSHeight([self bounds]));
    dividerRect = NSInsetRect(dividerRect, -2, 0);
    NSPoint mouseLocationInMyCoords = [self convertPoint:mouseLocation fromView:nil];
    if (![self mouse:mouseLocationInMyCoords inRect:dividerRect]) 
    {
        mouseLocation = [window convertBaseToScreen:mouseLocation];
        NSPoint origin = [window frame].origin;
        // Now we loop handling mouse events until we get a mouse up event.
        while ((theEvent = [NSApp nextEventMatchingMask:NSLeftMouseDownMask|NSLeftMouseDraggedMask|NSLeftMouseUpMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES])&&([theEvent type]!=NSLeftMouseUp)) 
        {
            @autoreleasepool 
            {
                NSPoint currentLocation = [window convertBaseToScreen:[theEvent locationInWindow]];
                origin.x += currentLocation.x-mouseLocation.x;
                origin.y += currentLocation.y-mouseLocation.y;
                // Move the window by the mouse displacement since the last event.
                [window setFrameOrigin:origin];
                mouseLocation = currentLocation;
            }
        }
        [self mouseUp:theEvent];
        return;
    }

    [super mouseDown:theEvent];
}

@end

第二个任务是同步两个拆分视图。使您的控制器类(可能是窗口控制器,但在您的代码中有意义的)成为您的主要内容拆分视图和标题栏拆分视图的代表。然后,实现下面的两个 NSSplitView 委托方法:

@implementation MyController
{
    BOOL updatingLinkedSplitview;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex
{
    // If already updating a split view, return early to avoid infinite loop and stack overflow
    if (updatingLinkedSplitview) return proposedPosition;

    if (splitView == self.mainSplitView)
    {
        // Main splitview is being resized, so manually resize the title bar split view
        updatingLinkedSplitview = YES;
        [self.titleBarSplitView setPosition:proposedPosition ofDividerAtIndex:dividerIndex];
        updatingLinkedSplitview = NO;
    }
    else if (splitView == self.titleBarSplitView)
    {
        // Title bar splitview is being resized, so manually resize the main split view
        updatingLinkedSplitview = YES;
        [self.mainSplitView setPosition:proposedPosition ofDividerAtIndex:dividerIndex];
        updatingLinkedSplitview = NO;
    }

    return proposedPosition;
}

- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize
{
    // This is to synchronize the splitter positions when the window is first loaded
    if (splitView == self.titleBarSplitView)
    {
        NSRect leftFrame = NSMakeRect(NSMinX([self.leftTitleBarView frame]),
                                      NSMinY([self.leftTitleBarView frame]),
                                      NSWidth([self.leftMainSplitView frame]),
                                      NSHeight([self.leftTitleBarView frame]));
        NSRect rightFrame = NSMakeRect(NSMaxX(leftFrame) + [splitView dividerThickness],
                                      NSMinY([self.rightTitleBarView frame]),
                                      NSWidth([self.rightMainSplitView frame]),
                                      NSHeight([self.rightTitleBarView frame]));

        [self.leftTitleBarView setFrame:leftFrame];
        [self.rightTitleBarView setFrame:rightFrame];
    }
}
于 2013-02-26T17:39:07.977 回答