栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Sprite Kit中的多点触控手势

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Sprite Kit中的多点触控手势

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

首先,声明字典

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

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

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 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 touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {    for touch in touches {        if selectedNodes[touch] != nil { selectedNodes[touch] = nil        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号