Swift Sprite套件中的多点触控手势
我正在使用XCode 6在Swift中使用Sprite工具包,我有许多不同的节点,但目前我只能检测一个手指并同时移动一个节点。 我想知道如何检测多个手指以便同时移动多个节点。 我的实际代码是:Swift Sprite套件中的多点触控手势,swift,sprite-kit,gesture,multi-touch,touches,Swift,Sprite Kit,Gesture,Multi Touch,Touches,我正在使用XCode 6在Swift中使用Sprite工具包,我有许多不同的节点,但目前我只能检测一个手指并同时移动一个节点。 我想知道如何检测多个手指以便同时移动多个节点。 我的实际代码是: var location = CGFloat() // finger position var actualNode = -1 // node touched by the finger, -1 means no node touched override func touchesBegan(touch
var location = CGFloat() // finger position
var actualNode = -1 // node touched by the finger, -1 means no node touched
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) // when a finger touch the screen
{
for touch: AnyObject in touches
{
location = touch.locationInNode(self) // we detect the finger position
}
for var index = 0; index < colorNode.count; index++
{
if nodeAtPoint(location) == colorNode[index].node
{
actualNode = index // the number of the node touched by the finger
}
}
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) // when a finger move
{
for touch: AnyObject in touches
{
location = touch.locationInNode(self) // we detect the finger position
}
if actualNode != -1 // if a node is touched
{
colorNode[actualNode].position = location // we move this node to the finger
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) // when a finger don't touch the screen anymore
{
actualNode = -1 // there is no node touched
}
var location=CGFloat()//手指位置
var actualNode=-1//手指触摸的节点,-1表示未触摸节点
override func touchsbegind(touchs:NSSet,withEvent:UIEvent)//当手指触摸屏幕时
{
用于触摸:触摸中的任何对象
{
location=touch.locationInNode(self)//我们检测手指的位置
}
对于var index=0;index
如您所见,我只有第一个手指的
位置,但如何检测多个手指的位置并将每个手指分配给手指接触的节点?同时移动多个节点相当简单。关键是独立跟踪每个触摸事件。一种方法是维护一个字典,使用触摸事件作为键,移动的节点作为值
首先,申报字典
var selectedNodes:[UITouch:SKSpriteNode] = [:]
将触摸到的每个精灵添加到字典中,并将触摸事件作为键
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in:self)
if let node = self.atPoint(location) as? SKSpriteNode {
// Assumes sprites are named "sprite"
if (node.name == "sprite") {
selectedNodes[touch] = node
}
}
}
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果let node=self.atPoint(位置)as?SKSpriteNode{
//假设精灵名为“精灵”
如果(node.name==“sprite”){
selectedNodes[触摸]=节点
}
}
}
}
根据需要更新精灵的位置
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in:self)
// Update the position of the sprites
if let node = selectedNodes[touch] {
node.position = location
}
}
}
override func touchesMoved(touch:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
//更新精灵的位置
如果let node=selectedNodes[触摸]{
node.position=位置
}
}
}
触摸结束时,从字典中删除精灵
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
if selectedNodes[touch] != nil {
selectedNodes[touch] = nil
}
}
}
override func touchesend(touch:Set,带有事件:UIEvent?){
接触{
如果选择了节点[触摸]!=nil{
selectedNodes[触摸]=无
}
}
}
记住将skView.isMultipleTouchEnabled设置为true,然后放弃上面的说明>同时移动多个节点相当简单。关键是独立跟踪每个触摸事件。。。。。。