Macos 在OS X上使用NSPangestureRecogener拖动SKNode时闪烁
今天,我决定尝试一些新的闪亮(受iOS启发)的东西添加到OSX:手势识别器和精灵工具包。我已经在SKView中设置了一个NSPangestureRecogener,并使用它来拖动添加到场景中的节点。但是,当快速拖动节点时,我看到一个奇怪的闪烁 代码非常简单,在AppDelegate中设置所有内容Macos 在OS X上使用NSPangestureRecogener拖动SKNode时闪烁,macos,cocoa,swift,sprite-kit,uigesturerecognizer,Macos,Cocoa,Swift,Sprite Kit,Uigesturerecognizer,今天,我决定尝试一些新的闪亮(受iOS启发)的东西添加到OSX:手势识别器和精灵工具包。我已经在SKView中设置了一个NSPangestureRecogener,并使用它来拖动添加到场景中的节点。但是,当快速拖动节点时,我看到一个奇怪的闪烁 代码非常简单,在AppDelegate中设置所有内容 func applicationDidFinishLaunching(aNotification: NSNotification) { // Insert code here to initi
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
// Add and scene to the view and create a root node
hostingView.presentScene(SKScene(size: CGSize(width: 1500.0, height: 1500.0)))
rootNode = SKNode()
// A a red circle
let node = SKShapeNode(circleOfRadius: 50.0)
node.lineWidth = 5.0
node.strokeColor = NSColor.whiteColor()
node.fillColor = NSColor.redColor()
node.userInteractionEnabled = true
rootNode?.addChild(node)
hostingView.scene?.addChild(rootNode!)
}
然后实现手势识别器的动作方法
@IBAction func panAction(sender: AnyObject) {
// Get the location in the view from the pan gesture recogniser
let viewPoint = (sender as NSPanGestureRecognizer).locationInView(hostingView)
// Convert from view -> scene -> root node coordinates
if let scene = hostingView.scene {
let scenePoint = hostingView.convertPoint(viewPoint, toScene:scene)
if let root = self.rootNode {
let rootNodePoint = scene.convertPoint(scenePoint, toNode: root)
let node = root.nodeAtPoint(rootNodePoint)
node.position = rootNodePoint
println("Drag to point:", NSStringFromPoint(scenePoint))
return
}
}
println("Node was nil.")
}
如果要运行此项目,当鼠标快速拖动并退出节点边界时,
nodeAtPoint:
正在返回背景节点。这导致了闪烁(谢谢@sangony)
解决方案是使用该值区分第一次触发时(nsgestureRecognitizerStateStart
),更新时(nsgestureRecognitizerStateChanged
),以及释放鼠标时(nsgestureRecognitizerStateEnded
)。通过检查这些值,即使鼠标移动到节点边界之外,也可以缓存并更新正确的节点
更新的操作方法是
@IBAction func panAction(sender: AnyObject) {
// Get the location in the view from the pan gesture recogniser
let recognizer = (sender as NSPanGestureRecognizer)
let viewPoint = recognizer.locationInView(hostingView)
if let scene = hostingView.scene {
// Convert from view -> scene
let scenePoint = hostingView.convertPoint(viewPoint, toScene:scene)
if let root = self.rootNode {
// Convert from scene -> rootNode
let rootNodePoint = scene.convertPoint(scenePoint, toNode: root)
// Use the recogniser state, this keeps track of the correct node
// even if the mouse has moved outside of the node bounds during
// the drag operation.
switch recognizer.state {
case .Began:
// Cache the clicked node
let node = root.nodeAtPoint(rootNodePoint)
if node != root {
node.position = rootNodePoint
draggedNode = node
return
}
case .Changed:
// Update the cached node position
if let draggedNode = self.draggedNode {
draggedNode.position = rootNodePoint
}
case .Ended:
// Finally update the position and clear the cache
if let draggedNode = self.draggedNode {
draggedNode.position = rootNodePoint
self.draggedNode = nil
}
default:
return
}
}
}
}
您是否尝试过直接将shapeNode添加到视图中,而不是首先将其添加到SKNode中?有趣的是,我认为您已经了解了一些内容。现在的问题不同了,当您这样做时,当鼠标移动到节点边界之外时,圆停止拖动。因此,之前的情况是,当鼠标快速移动时,我们将圆形节点拖动到根节点。