我移动鼠标时没有调用mouseMoved函数?

时间:2017-03-28 03:08:53

标签: swift3 mousemove swift-playground nstrackingarea

我试图在SKScene中找到鼠标坐标,但是,没有调用moveMouse函数。 (顺便说一下,这是在Swift Playground中) I甚至写了一个打印函数,测试该函数是否被调用,但它什么都没打印。

这就是我设置NSTrackingArea的方式:

    let options = [NSTrackingAreaOptions.mouseMoved, NSTrackingAreaOptions.activeInKeyWindow, NSTrackingAreaOptions.activeAlways, NSTrackingAreaOptions.inVisibleRect, ] as NSTrackingAreaOptions
    let tracker = NSTrackingArea(rect: viewFrame, options: options, owner: self.view, userInfo: nil)
    self.view?.addTrackingArea(tracker)

这是mouseMoved函数(未被调用的函数)

override public func mouseMoved(with event: NSEvent) {

        point = event.location(in: self)
        print(point)
    }

是否有理由不调用mouseMoved?

1 个答案:

答案 0 :(得分:0)

我使用以下代码创建了一个游乐场(只有该代码):

import AppKit
import SpriteKit
import PlaygroundSupport

class Scene:SKScene {
    override public func mouseMoved(with event: NSEvent) {
        let point = event.location(in: self)
        print(point)
    }
}

let frame = CGRect(x:0, y:0, width:1920, height:1080)
let view = SKView(frame:frame)
let scene = Scene(size: CGSize(width: 1080, height: 1080))
scene.backgroundColor = #colorLiteral(red: 0.4078431373, green: 0.7843137255, blue: 0.6509803922, alpha: 1)
scene.scaleMode = .aspectFit

let options = [NSTrackingAreaOptions.mouseMoved, NSTrackingAreaOptions.activeInKeyWindow, NSTrackingAreaOptions.activeAlways, NSTrackingAreaOptions.inVisibleRect, ] as NSTrackingAreaOptions
let tracker = NSTrackingArea(rect:frame, options: options, owner:view, userInfo: nil)
view.addTrackingArea(tracker)

PlaygroundPage.current.needsIndefiniteExecution = true
view.presentScene(scene)
PlaygroundPage.current.liveView = view

然后,我点击"显示助理编辑器"打开了游乐场时间轴视图。工具栏中的按钮。我还打开了Debug区域,以便能看到控制台。

此时,时间轴视图显示绿色视图。我将鼠标指针移到绿色视图上,我可以看到鼠标坐标在控制台中打印出来。所以,据我所知,上面的代码工作正常。

你可以试试你的代码,看看会发生什么吗?