touchesBegan,如何获得所有接触,不只是第一次或最后一次?(touchesBegan, how to get all touches, not just first or last?)

编程入门 行业动态 更新时间:2024-10-25 16:28:41
touchesBegan,如何获得所有接触,不只是第一次或最后一次?(touchesBegan, how to get all touches, not just first or last?)

在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 // etc

This 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 = true

You'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)) }

更多推荐

本文发布于:2023-08-07 01:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458796.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   touchesBegan   touches

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!