【问题标题】:Drag and drop animation recoils back to source拖放动画后坐回源
【发布时间】:2014-02-28 20:06:16
【问题描述】:

将动画从 NSTableView 拖放到另一个窗口的 NSTextView 会退回到源视图而不是文本视图。否则,拖放操作可以正常工作——我的数据被粘贴了。我的 NSTextView 的子类在阳光下拥有所有的拖放协议方法(见下面的代码)。可能出了什么问题?

- (void)awakeFromNib { // Adjust default insets
[self setTextViewInset];
[self registerForDraggedTypes:[NSArray arrayWithObjects:AWNDragNDropGeneralRuleRecordType, nil]]; 
[self registerAsObserver];
}

- (void)registerAsObserver
{
    [self addObserver:scrollerSubclass
           forKeyPath:@"focused"
              options:NSKeyValueObservingOptionNew
              context:NULL];
}

- (BOOL)acceptsFirstResponder
{
    NSLog(@"Accepting");
    [self setFocused:YES];
    return YES;
}

- (BOOL)resignFirstResponder
{
    NSLog(@"Resigning");
    [self setFocused:NO];
    [super resignFirstResponder]; // Otherwise cursor remains in textView
    return YES;
}

- (BOOL)becomeFirstResponder
{
    NSLog(@"Becoming");
    return YES;
}

- (void)setFocused:(BOOL)x
{
    NSLog(@"-setFocused: is called with %d",x);
    focused = x;
}


- (BOOL)focused
{
    NSLog(@"-focused: is returning %d",focused);
    return focused;
}


- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    NSLog(@"prepareForDragOperation YES");
    return YES;
}


- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSLog(@"draggingEntered:");
    if ([sender draggingSource] == self) {
        return NSDragOperationNone;
    }

    return NSDragOperationCopy;
}


- (void)draggingExited:(id <NSDraggingInfo>)sender
{
    NSLog(@"Draging Exited:");
    [self setNeedsDisplay:YES];
}


- (void)draggingEnded:(id < NSDraggingInfo >)sender
{
    [self performDragOperation:sender];     
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{ // Look for drag source in 'SpellRuleFiles.SpellRulesWindowDelegate tableView delegate method
    NSPasteboard *pb = [sender draggingPasteboard];

    if (![self readFromPasteboard:pb]) {
        NSLog(@"Error: Could not read from dragging pasteboard");
        NSLog(@"performDragOperation NO");
        return NO;
    }
    NSLog(@"performDragOperation YES");
    return YES;
}


- (BOOL)readFromPasteboard:(NSPasteboard *)pb 
{ // Source of paste data is 'SpellRuleFile's ' 

    // I'm not showing this code

    return YES;     
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    NSLog(@"conclude drag operation:");
    [self setNeedsDisplay:YES];
}

【问题讨论】:

    标签: cocoa drag-and-drop


    【解决方案1】:

    在 -prepareForDragOperation: 你想设置 sender.animatesToDestination = NO,在这种情况下,被拖动的东西会在你放下它们时消失,而不是后退。

    或者,如果你想要更好但更复杂的东西,你可以使用 -enumerateDraggingItemsWithOptions:etc:etc: 来检查每个被放到你的文本视图上的项目并适当地设置它们的 draggingFrame,在这种情况下事情被拖动的将动画到您选择的最终目的地。

    【讨论】:

    • 感谢您的建议,但动画仍然后坐力。我的预感是,通过使用 NSTextView 的子类,它的窗口 First Responder 不会对拖动事件做出反应。我不确定如何处理这种可能性。
    • 我遇到了同样的问题。你找到解决办法了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多