Sprite Kit中的多点触控手势 [英] Multi-touch gesture in Sprite Kit

查看:106
本文介绍了Sprite Kit中的多点触控手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用XCode 6的Sprite-Kit在Swift中工作,我有许多不同的节点,但是目前,我只设法检测到一根手指并同时移动一个节点. 我想知道如何设法检测多个手指以便同时移动多个节点. 我的实际代码是:

I'm working in Swift with Sprite-Kit using XCode 6, and I have many different nodes but for the moment I only manage to detect one finger and move one node at the same time. I want to know how could I manage to detect multiple fingers in order to move multiple nodes at the same time. My actual code is :

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
}

如您所见,我只有第一根手指的position,但是如何检测多个手指的位置并将每个手指分配给手指触摸的节点?

As you can see I only have the position of my first finger, but how can I detect multiple finger position and assign each finger to the node touched by the finger ?

推荐答案

同时移动多个节点非常简单.关键是要独立跟踪每个触摸事件.一种方法是维护一个字典,该字典使用touch事件作为键,并使用要移动的节点作为值.

Moving multiple nodes simultaneously is fairly straightforward. The key is to track each touch event independently. One way to do that is to maintain a dictionary that uses the touch event as the key and the node being moved as the value.

首先,声明字典

var selectedNodes:[UITouch:SKSpriteNode] = [:]

将每个带有触摸事件的精灵添加到字典中,并以touch事件作为键

Add each sprite touched to the dictionary with the touch event as the key

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
            }
        }
    }
}

根据需要更新子画面的位置

Update the positions of the sprites as needed

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
        }
    }
}

触摸结束后从词典中删除精灵

Remove the sprite from the dictionary when the touch ends

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        if selectedNodes[touch] != nil {
            selectedNodes[touch] = nil
        }
    }
}

这篇关于Sprite Kit中的多点触控手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆