1

是否可以提供可拖动的 NSTabViewItem,
基本上是我想要的,如果我在 NSTabViewITem 的标签上按下 L 按钮并移动,我应该允许拖动 TabView 项目,我想为 NSTabView 项目的移动做这件事并拥有更多功能,如果用户拖动 NSTabView 项目的标签并将其移动到特定区域,那么我应该允许删除该 NSTabView 项目,

我只能找到一种拥有 PSMTab 栏的方法,但我在 NSTabView 项目上还有其他功能,如果我采用这种方法,我将丢失这些功能。

4

1 回答 1

1

感谢您调查它,不知何故我可以做到......发布一些重要的代码......

1 -- 必须有自定义 TabView 类来处理鼠标事件。

// 接口贴在下面,

#import <Cocoa/Cocoa.h>


typedef enum __itemDragState{
    itemNotDragging = 0,
    itemDragStatNormal = 0,
    itemDragging    = 1,
    itemDropped     = 2
} ItemDragStat;

@protocol CustomTabViewDelegate <NSObject>

@required
-(bool)allowDrag;
-(bool)allowDrop;
-(void)dragEnter;
-(void)acceptDrop;
-(void)draggingCancelled;
-(void)itemDropped:(id)draggedTabViewItem;
-(void)itemDroppedCompleted:(id)droppedTabViewItem;
@end

@interface CustomTab : NSTabView{
    ItemDragStat eItemDragStat; 
    id draggedItem;
}

@property(assign)id draggedItem;
@end

现在一些重要的实现

#import "CustomTab.h"
#include "Log.h"

@implementation CustomTab

@synthesize draggedItem;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }

    return self;
}

# if 0
// don't delete it, might need later on
- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}
# endif
- (void)mouseUp:(NSEvent *)theEvent{
    log(" Mouse up ");


    NSPoint location = [self convertPoint: [theEvent locationInWindow]
                                 fromView: nil];



    NSTabViewItem *anItem = [self tabViewItemAtPoint: location];

    if ( anItem == nil ) {
        // if its mouse up else where, reject dragging regardless
        eItemDragStat = itemDragStatNormal;
        log("Item will not be dropped");
        return;
    }

    if ( ![anItem isEqual:[self selectedTabViewItem]]){
        log("Mouse up is in nonselected item");

        if ( eItemDragStat == itemDragging){
            log("Item will be dropped into this ");


            id droppedTabViewItem = anItem;

            if ( droppedTabViewItem && [droppedTabViewItem respondsToSelector:@selector(itemDropped:)]){

                id selectedTabViewItem = [self selectedTabViewItem];

                [droppedTabViewItem performSelector:@selector(itemDropped:) withObject:selectedTabViewItem];

            }
        }
    }
    eItemDragStat = itemDragStatNormal;
    //   return;

    //    [super mouseUp:theEvent];
}
- (void)mouseDown:(NSEvent *)theEvent{

    NSPoint location = [self convertPoint: [theEvent locationInWindow]
                                 fromView: nil];

    draggedItem = [self tabViewItemAtPoint:location];

    NSTabViewItem *anItem = [self tabViewItemAtPoint: location];

    if (anItem != nil  &&  ![anItem isEqual: [self selectedTabViewItem]])
    {
        [self selectTabViewItem: anItem];
    }


}

- (void)mouseDragged:(NSEvent *)theEvent{

    NSPoint location = [self convertPoint: [theEvent locationInWindow]
                                 fromView: nil];


    id tabViewItemId = [self tabViewItemAtPoint:location];

    NSTabViewItem *anItem = [self tabViewItemAtPoint: location];

    if (anItem){
        if (![anItem isEqual:draggedItem]){

            if (tabViewItemId && [tabViewItemId respondsToSelector:@selector(allowDrag)]){
                eItemDragStat = itemDragging;

            }else{
                // drag will be cancelled now.
                // tell client item to stop dragging
                if (eItemDragStat == itemDragging){
                    if ( draggedItem && [ draggedItem respondsToSelector:@selector(draggingCancelled)]){

                        [draggedItem performSelector:@selector(draggingCancelled)];
                        draggedItem = nil;
                    }
                    }
                eItemDragStat = itemNotDragging;
                // if we have +cursor then it should be reset
            }

        }else{
           log(" Mouse dragged");
        }
    }else{
        // dragging went elsewhere, lets close this dragging operation
        if ( draggedItem && [ draggedItem respondsToSelector:@selector(draggingCancelled)]){

            [draggedItem performSelector:@selector(draggingCancelled)];
            draggedItem = nil;
        }
        // here reset the mouse pointer
         eItemDragStat = itemNotDragging;

    }
}

@end

它需要一些更精细的调整,并且还在继续......

于 2013-02-12T12:03:52.770 回答