在SpriteKit的SKSpriteNodes和其他可见节点中,对这些节点中的触摸作出响应通常以这种方式进行:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first {... // do something with the touch location that's now in touch // etc这一切都很棒,但如果我想确保获得其中任何和所有触摸的触摸信息(让我们想象一下......)SKSpriteNode怎么办?
= touches.first限制条件只响应此SKSpriteNode内的第一次触摸。 它是如何处理这个SKSpriteNode中的所有触摸的,这样每个触摸点都有其可用的位置,或者其它任何其他位置都可用,并且可以随身携带。
我知道。 愚蠢的问题。 我是那个人。 我根本看不出这是如何完成的。
In SpriteKit's SKSpriteNodes and other visible nodes, responding to touches in those nodes is often done in some manner like this:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first {... // do something with the touch location that's now in touch // etcThis is all great, but what if I want to make sure I get the touch info of any and all touches inside this (let's imagine a...) SKSpriteNode?
= touches.first limits the conditional to responding only to the first touch inside this SKSpriteNode. How is it done for all touches in this SKSpriteNode, such that each has its location available, or whatever else it's got and carrying around.
I know. Stupid question. I'm that guy. I simply can't see how this is done.
最满意答案
通常情况下,视图不会响应多个触摸,除非multipleTouchEnabled属性设置为true 。
mySprite.multipleTouchEnabled = true您会注意到方法touchesBegan:event:不返回单个UITouch对象,而是返回UITouch对象的Set(又名无序集合)。
在此方法内部,您可以(例如)遍历该集合以检查每次触摸的条件,而不是仅仅从集合中获得第一次触摸。
for touch in touches { // do something with the touch object print(touch.location(mySKNodeObject)) }Usually a view doesn't respond to multiple touches unless the multipleTouchEnabled property is set to true.
mySprite.multipleTouchEnabled = trueYou'll notice that the method touchesBegan:event: doesn't return a single UITouch object, but a Set (a.k.a. an unordered collection) of UITouch objects.
Inside this method, instead of just getting the first touch out of the set, you could (for example) iterate over the set in order to check a condition on every touch.
for touch in touches { // do something with the touch object print(touch.location(mySKNodeObject)) }更多推荐
发布评论