我试图让 click-though 工作,以便我的应用程序即使在窗口处于非活动状态时也能响应鼠标点击。我有一个带有响应鼠标事件的 NSCollectionView 项目的 collectionView。我尝试将 collectViewItem 使用的视图子类化,并且还子类化了用于 NSCollectionView 的视图。它不起作用。子类视图的代码如下所示:
import Cocoa
class SubclassedView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
// adding this override should allow click-through so buttons can be clicked even with an inactive window
// it doesn't work
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
print("got first mouse")
return true
}
override var acceptsFirstResponder :Bool {
get {
return true
}
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这里出了什么问题?
我花了几天时间尝试了很多不同的东西来完成这项工作,但一直失败。这开始变得令人沮丧,我真的很想知道如何让用户拖放工作以重新排序 NSCollectionView 中的项目的人的想法。这是我的最新尝试,它完全没有任何作用:
let MyItemType = "myItemType"
class CollectionViewController: NSViewController {
@IBOutlet weak var collectionView: NSCollectionView!
let MyItemType = "myItemType"
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
loadDevices()
getstate()
// try to register drag and drop (all attempts fail)
//collectionView.register(forDraggedTypes: [MyItemType, NSFilenamesPboardType]
}
// more functions here were omitted...
}
extension CollectionViewController : NSCollectionViewDataSource {
func numberOfSections(in collectionView: NSCollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return groupbuttons.count
}
func collectionView(_ itemForRepresentedObjectAtcollectionView: …
Run Code Online (Sandbox Code Playgroud)