swift – 如何在spritekit中创建垂直滚动菜单?

swift – 如何在spritekit中创建垂直滚动菜单?,第1张

概述我想用我的游戏(In SpriteKit)创建一个带有按钮和图像的商店,但是我需要这些项目可以滚动,这样玩家就可以在商店中上下滚动(就像一个UITableView,但每个都有多个SKSpriteNodes和SKLabelNodes)细胞).知道如何在SpriteKit中做到这一点吗? 承诺的第二个答案,我只是想出了问题. 我建议总是从我的gitHub项目中获取此代码的最新版本,因为我做了更改,因为 我想用我的游戏(In SpriteKit)创建一个带有按钮和图像的商店,但是我需要这些项目可以滚动,这样玩家就可以在商店中上下滚动(就像一个UItableVIEw,但每个都有多个SKSpriteNodes和SKLabelNodes)细胞).知道如何在SpriteKit中做到这一点吗? 承诺的第二个答案,我只是想出了问题.

我建议总是从我的gitHub项目中获取此代码的最新版本,因为我做了更改,因为这个答案,链接在底部.

第1步:创建一个新的swift文件并粘贴此代码

import SpriteKit/// Scroll directionenum ScrollDirection {    case vertical // cases start with small letters as I am following Swift 3 guildlines.    case horizontal}class CustomScrollVIEw: UIScrollVIEw {// MARK: - Static PropertIEs/// touches allowedstatic var Disabledtouches = false/// Scroll vIEwprivate static var scrollVIEw: UIScrollVIEw!// MARK: - PropertIEs/// Current sceneprivate let currentScene: SKScene/// Moveable nodeprivate let moveableNode: SKNode/// Scroll directionprivate let scrollDirection: ScrollDirection/// touched nodesprivate var nodestouched = [AnyObject]()// MARK: - Initinit(frame: CGRect,scene: SKScene,moveableNode: SKNode) {    self.currentScene = scene    self.moveableNode = moveableNode    self.scrollDirection = scrollDirection    super.init(frame: frame)    CustomScrollVIEw.scrollVIEw = self    self.frame = frame    delegate = self    indicatorStyle = .White    scrollEnabled = true    userInteractionEnabled = true    //canCancelContenttouches = false    //self.minimumZoomScale = 1    //self.maximumZoomScale = 3    if scrollDirection == .horizontal {        let flip = CGAffinetransformMakeScale(-1,-1)        transform = flip    }}required init?(coder aDecoder: NSCoder) {    fatalError("init(coder:) has not been implemented")   }}// MARK: - touchesextension CustomScrollVIEw {/// BeganoverrIDe func touchesBegan(touches: Set<UItouch>,withEvent event: UIEvent?) {    for touch in touches {        let location = touch.locationInNode(currentScene)        guard !CustomScrollVIEw.Disabledtouches else { return }        /// Call touches began in current scene        currentScene.touchesBegan(touches,withEvent: event)        /// Call touches began in all touched nodes in the current scene        nodestouched = currentScene.nodesAtPoint(location)        for node in nodestouched {            node.touchesBegan(touches,withEvent: event)        }    }}/// MovedoverrIDe func touchesMoved(touches: Set<UItouch>,withEvent event: UIEvent?) {    for touch in touches {        let location = touch.locationInNode(currentScene)        guard !CustomScrollVIEw.Disabledtouches else { return }        /// Call touches moved in current scene        currentScene.touchesMoved(touches,withEvent: event)        /// Call touches moved in all touched nodes in the current scene        nodestouched = currentScene.nodesAtPoint(location)        for node in nodestouched {            node.touchesMoved(touches,withEvent: event)        }    }}/// EndedoverrIDe func touchesEnded(touches: Set<UItouch>,withEvent event: UIEvent?) {    for touch in touches {        let location = touch.locationInNode(currentScene)        guard !CustomScrollVIEw.Disabledtouches else { return }        /// Call touches ended in current scene        currentScene.touchesEnded(touches,withEvent: event)        /// Call touches ended in all touched nodes in the current scene        nodestouched = currentScene.nodesAtPoint(location)        for node in nodestouched {            node.touchesEnded(touches,withEvent: event)        }    }}/// CancelledoverrIDe func touchesCancelled(touches: Set<UItouch>?,withEvent event: UIEvent?) {    for touch in touches! {        let location = touch.locationInNode(currentScene)        guard !CustomScrollVIEw.Disabledtouches else { return }        /// Call touches cancelled in current scene        currentScene.touchesCancelled(touches,withEvent: event)        /// Call touches cancelled in all touched nodes in the current scene        nodestouched = currentScene.nodesAtPoint(location)        for node in nodestouched {            node.touchesCancelled(touches,withEvent: event)        }     }   }}// MARK: - touch Controlsextension CustomScrollVIEw {     /// disable    class func disable() {        CustomScrollVIEw.scrollVIEw?.userInteractionEnabled = false        CustomScrollVIEw.Disabledtouches = true    }    /// Enable    class func enable() {        CustomScrollVIEw.scrollVIEw?.userInteractionEnabled = true        CustomScrollVIEw.Disabledtouches = false    }}// MARK: - Delegatesextension CustomScrollVIEw: uiscrollviewdelegate {    func scrollVIEwDIDScroll(scrollVIEw: UIScrollVIEw) {        if scrollDirection == .horizontal {            moveableNode.position.x = scrollVIEw.contentOffset.x        } else {            moveableNode.position.y = scrollVIEw.contentOffset.y        }    }}

这将构成UIScrollVIEw的子类并设置它的基本属性.它有自己的触摸方法,它被传递到相关的场景.

第2步:在您想要使用它的相关场景中,您可以创建滚动视图和可移动节点属性

weak var scrollVIEw: CustomScrollVIEw!let moveableNode = SKNode()

并将它们添加到dIDMovetoVIEw中的场景中

scrollVIEw = CustomScrollVIEw(frame: CGRect(x: 0,y: 0,wIDth: self.frame.size.wIDth,height: self.frame.size.height),scene: self,moveableNode: moveableNode,scrollDirection: .vertical)scrollVIEw.contentSize = CGSizeMake(self.frame.size.wIDth,self.frame.size.height * 2)vIEw?.addSubvIEw(scrollVIEw) addChild(moveableNode)

您在第1行中执行的 *** 作是使用场景维度初始化滚动视图助手.您还传递场景以供参考,以及您在步骤2中创建的moveableNode.
第2行是设置scrollVIEw的内容大小的地方,在这种情况下,它是屏幕高度的两倍.

第3步: – 添加标签或节点等,并定位它们.

label1.position.y = CGRectGetMIDY(self.frame) - self.frame.size.heightmoveableNode.addChild(label1)

在此示例中,标签将位于scrollVIEw的第2页上.这是您必须使用标签和定位的地方.

我建议如果滚动视图中有很多页面,并且有很多标签要执行以下 *** 作.在滚动视图中为每个页面创建一个SKSpriteNode,并使每个页面都与屏幕大小相同.将它们称为page1Node,page2Node等.您可以将第二页上所需的所有标签添加到page2Node.这里的好处是你基本上可以像往常一样在page2Node中定位你所有的东西,而不仅仅是在scrollVIEw中定位page2Node.

你也很幸运,因为垂直使用scrollVIEw(你说你想要的)你不需要做任何翻转和反向定位.

我制作了一些类func,所以如果你需要禁用你的scrollVIEw,你可以在scrollVIEw上覆盖另一个菜单.

CustomScrollVIEw.enable()CustomScrollVIEw.disable()

最后不要忘记在转换到新场景之前从场景中删除滚动视图.在spritekit中处理UIKit时的痛苦之一.

scrollVIEw?.removeFromSuperVIEw()

对于水平滚动,只需将init方法上的滚动方向更改为.horizo​​ntal(步骤2).

而现在最大的痛苦是在定位东西时一切都在逆转.所以滚动视图从右到左.因此,您需要使用scrollVIEw“contentOffset”方法重新定位它,并且基本上从右到左以相反的顺序放置所有标签.一旦了解了发生的事情,再次使用SkNodes可以更轻松.

希望这对大量帖子有帮助和抱歉,但正如我所说,spritekit有点痛苦.让我知道它是怎么回事,如果我错过了什么.

项目在gitHub上

https://github.com/crashoverride777/SwiftySKScrollView

总结

以上是内存溢出为你收集整理的swift – 如何在spritekit中创建垂直滚动菜单?全部内容,希望文章能够帮你解决swift – 如何在spritekit中创建垂直滚动菜单?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1032535.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存