
一个gif来展示这个问题:
代码从上面的例子(或Xcode Project):
import Cocoaclass AppDelegate: NSObject,NSApplicationDelegate { func applicationDIDFinishLaunching(aNotification: NSNotification?) { let vc = StatusItemVIEwController() } func applicationWillTerminate(aNotification: NSNotification?) { // Insert code here to tear down your application }}class StatusItemVIEw: NSVIEw,NSDraggingDestination { public var image = NSImage(named: "NSQuickLookTemplate") public var action: Selector? public var target: AnyObject? private var statusItem: NsstatusItem init(statusItem: NsstatusItem) { let itemWIDth = statusItem.length let itemHeight = Nsstatusbar.systemStatusbar().thickness self.statusItem = statusItem super.init(frame: NSRect(x: 0.0,y: 0.0,wIDth: itemWIDth,height: itemHeight)) var types = [NSURLPboardType,NsstringPboardType] as [String] for type in NSImage.imagePasteboardTypes() { types.append(type as String) } self.registerForDraggedTypes(types) statusItem.vIEw = self } overrIDe func drawRect(dirtyRect: NSRect) { let iconSize = image.size let bounds = self.bounds let iconX = round((bounds.wIDth - iconSize.wIDth)/2) let iconY = round((bounds.height - iconSize.height)/2) image.drawAtPoint(NSPoint(x: iconX,y: iconY),fromrect: NSRect(),operation: NSCompositingOperation.CompositeOverlay,fraction: 1.0) } overrIDe func mouseDown(theEvent: NSEvent!) { if let _action = self.action { NSApp.sendAction(_action,to: self.target,from: self) } } overrIDe func performDragOperation(sender: NSDraggingInfo!) -> Bool { println("Performed") return true } overrIDe func draggingEntered(sender: NSDraggingInfo!) -> NSDragOperation { return NSDragOperation.copy } overrIDe func draggingUpdated(sender: NSDraggingInfo!) -> NSDragOperation { println("Dragging") return NSDragOperation.copy } overrIDe func draggingExited(sender: NSDraggingInfo!) { println("Exit") }}class StatusItemVIEwController: NSObject { private var statusbarItem: NsstatusItem? private var statusItemVIEw: StatusItemVIEw! init() { self.statusbarItem = Nsstatusbar.systemStatusbar().statusItemWithLength(24) super.init() if let item = statusbarItem { statusItemVIEw = StatusItemVIEw(statusItem: item) statusItemVIEw.target = self statusItemVIEw.action = "doAction:" } else { println("Unable to create status item,terminating self") NSApplication.sharedApplication().terminate(nil) } } deinit { Nsstatusbar.systemStatusbar().removeStatusItem(statusbarItem) statusbarItem = nil statusItemVIEw = nil } func doAction(sender: AnyObject!) { println("Here is where one would perform the action") }} 也许这是一个错误,也许我只是错过了一些东西.为什么会发生这种情况,有什么可以做的,以防止这种行为?
看起来像是一个BUG.我刚刚使用XCode 6.1.1和OS X 10.10.2测试了您的项目,并且药物工作很好. 总结以上是内存溢出为你收集整理的可可 – NSStatusItem自定义视图NSDraggingDestination仅在点击项目后才能工作全部内容,希望文章能够帮你解决可可 – NSStatusItem自定义视图NSDraggingDestination仅在点击项目后才能工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)