【问题标题】:Examples of NSWindow's dragImage?NSWindow的dragImage的例子?
【发布时间】:2011-05-25 00:39:53
【问题描述】:

我正在寻找NSWindow的dragImage方法的解释。

- (void)dragImage:(NSImage *)image at:(NSPoint)imageLocation offset:(NSSize)pointerOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pasteboard source:(id)sourceObject slideBack:(BOOL)slideBack

有什么帮助吗?

【问题讨论】:

    标签: objective-c drag-and-drop draggable


    【解决方案1】:

    该方法的 NSView 文档提供了更详细的解释,包括示例代码。这两种方法的唯一区别是imageLocation所在的坐标系。在http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html#//apple_ref/doc/uid/20000014-SW135找到它

    编辑:这是来自该链接的示例代码,添加了 cmets。

    - (void)mouseDown:(NSEvent *)theEvent {
        // Create an offset for the offset parameter. Since it is ignored, you should just use 0,0.
        NSSize dragOffset = NSMakeSize(0.0, 0.0);
        NSPasteboard *pboard;
    
        // Get the pasteboard used to hold drag-and-drop data
        pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
        // Declare the data type used on the pasteboard. This sample passes a TIFF representation of an image
        [pboard declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]  owner:self];
        // Add the data to the pasteboard.
        [pboard setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType];
        // Call dragImage:... with:
        // An image representing the object you are dragging. This could be a file's icon or a screenshot of a view, etc.
        [self dragImage:[self image]
        // The starting location of the image in this views coordinates. This should be close to the location of the object you are dragging.
                     at:[self imageLocation]
        // An NSSize structure. This is not used.
                 offset:dragOffset
        // The mousedown event
                  event:theEvent
        // The pasteboard which contains the data
             pasteboard:pboard
        // The object providing the data
                 source:self
        // Whether or not the image should slide to its starting location if the drag isn't completed.
              slideBack:YES];
    
    }
    

    源对象(本例中为self)需要实现NSDraggingSource 协议。您可能还对NSPasteboard Class Reference 感兴趣。

    【讨论】:

    • 太好了,这正是我想要的。非常感谢您加倍努力。
    猜你喜欢
    • 1970-01-01
    • 2021-03-30
    • 2011-05-17
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2013-10-26
    相关资源
    最近更新 更多