自从我完成任何 Cocoa 开发以来已经有一段时间了,我正试图将这个非常快速和肮脏(且简单)的应用程序包装起来。我决定使用 MacRuby,因为这是学习它的一个很好的借口,而且该应用程序非常简单,很有意义。
不过,我在获取自定义 View 来响应拖动事件时遇到了问题。
class ImportPanel < Panel
def initWithFrame(frame)
registerForDraggedTypes(NSArray.arrayWithObjects(NSPasteboardTypeSound, nil))
super(frame)
end
def mouseDown(event)
NSLog('click')
end
def draggingEntered(sender)
NSLog('drag')
end
end
在本例中,Panel 只是一个添加了边框的 NSView。此自定义 View (ImportPanel) 正确响应单击事件,但根本不响应拖动事件。我为 registerForDraggedTypes: 尝试了几种不同的粘贴板类型和配置,但似乎都没有产生任何结果。
最佳答案
这段代码对我有用。
AppDelegate.rb
class AppDelegate
attr_accessor :window
attr_accessor :panel
def applicationDidFinishLaunching(a_notification)
# nothing special here
end
def initialize
@panel = Panel.new
# Just for debug
puts @panel
end
end
这是我的player.rb
:
class Panel < NSView
def awakeFromNib
registerForDraggedTypes(NSArray.arrayWithObjects(NSFilenamesPboardType, NSURLPboardType, NSStringPboardType, nil))
end
def mouseDown(event)
NSLog('click')
end
def draggingEntered(sender)
NSLog('drag')
return NSDragOperationNone
end
end
拖动类型的数组就是这样帮助我测试不同的拖动操作(URL、文件等)。请注意,draggingEntered 需要返回一个 NSDragOperation
我使用 NSDragOperationNone
只是为了看看它是否有效。
关于ruby - MacRuby 应用程序中的拖动事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8126974/