MyView具有用户提供的文本,图像等。目标是让用户能够将图像从那里拖动到图像可以去的任何地方。代码是从GitHub示例定制的。代码的每个部分都按预期工作 - NSDraggingSource协议中的所有函数都在预期时被调用 - 并且拖动的图像看起来就像它拖动时一样。但是在任何目的地都没有下降,例如,我可以删除从其他应用程序拖动的图像。我错过了什么防止跌落?
class MyView: NSView, NSDraggingSource {
var mouseDownEvent: NSEvent?
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}
func draggingSession(session: NSDraggingSession, sourceOperationMaskForDraggingContext context: NSDraggingContext) -> NSDragOperation {
return .Copy
}
func draggingSession(session: NSDraggingSession, endedAtPoint screenPoint: NSPoint, operation: NSDragOperation) {
}
func draggingSession(session: NSDraggingSession, willBeginAtPoint screenPoint: NSPoint) {
}
func draggingSession(session: NSDraggingSession, movedToPoint screenPoint: NSPoint) {
}
override func mouseDown(theEvent: NSEvent) {
self.mouseDownEvent = theEvent
}
override func mouseDragged(theEvent: NSEvent) {
let mouseDown = self.mouseDownEvent!.locationInWindow
var point: NSPoint = theEvent.locationInWindow
point = anotherView!.convertPoint(point, toView: nil)
if CGRectContainsPoint(someObject.rect, point) {
let image = (someObject.contents as! NSImage)
let draggingItem = NSDraggingItem(pasteboardWriter: image)
let draggingFrameOrigin = convertPoint(mouseDown, fromView: nil)
let draggingFrame = NSRect(origin: draggingFrameOrigin, size: image.size).offsetBy(dx: -image.size.width / 2, dy: -image.size.height / 2)
draggingItem.draggingFrame = draggingFrame
draggingItem.imageComponentsProvider = {
let component = NSDraggingImageComponent(key: NSDraggingImageComponentIconKey)
component.contents = image
component.frame = NSRect(origin: NSPoint(), size: draggingFrame.size)
return [component]
}
beginDraggingSessionWithItems([draggingItem], event: mouseDownEvent!, source: self)
}
}
}
可能与NSView
而不是NSImageView
的拖动有关吗?我向image
提供draggingItem
,但这只是为了拖动视觉而不是删除的内容?
谢谢!