ios - SpriteKit 自定义 UIScrollView 加速

标签 ios uiscrollview sprite-kit acceleration

基本上我需要在我的 SpriteKit 项目中创建一个 UIScrollView,但是我在添加 SKButtons(用于按钮管理的自定义 SKNode 类)时遇到了很多问题。所以我继续创建一个带有触摸手势的可滚动 SKNode,但显然,这个栏不会有原生的 UIScrollView 加速:我一直在寻找的功能。

因此尝试通过添加 native UIScrollView 来解决这个问题并捕获位置的每个变化,如下所示:

enter image description here

使用此代码:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [blueNode setPosition:CGPointMake(blueNode.position.x, scrollerUI.contentOffset)];
}

这是正确的,但愚蠢的是我忘记了如果我添加按钮,触摸手势将无法识别按钮的触摸事件! (UIScrollView 优先)。

也许这只是一个愚蠢的问题,但我真的不知道如何解决。也许编写我自己的加速方法?

最佳答案

我想我想出了一个处理触摸的解决方案。由于 UIScrollView 吃掉所有触摸以允许它滚动,您需要将它们传递给 SKScene。您可以通过子类化 UIScrollView 来做到这一点:

class SpriteScrollView: UIScrollView {

    var scene: SKScene?


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        scene?.touchesBegan(touches, withEvent: event)
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        scene?.touchesMoved(touches, withEvent: event)
    }

    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        scene?.touchesCancelled(touches, withEvent: event)
    }

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        scene?.touchesEnded(touches, withEvent: event)
    }
}

然后您的场景需要查看触摸是否触及任何节点并将触摸适本地发送到这些节点。所以在你的 SKScene 添加这个:

var nodesTouched: [AnyObject] = []

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)

    let location = (touches.first as! UITouch).locationInNode(self)
    nodesTouched = nodesAtPoint(location)

    for node in nodesTouched as! [SKNode] {
        node.touchesBegan(touches, withEvent: event)
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesMoved(touches, withEvent: event)

    for node in nodesTouched as! [SKNode] {
        node.touchesMoved(touches, withEvent: event)
    }
}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    super.touchesCancelled(touches, withEvent: event)
    for node in nodesTouched as! [SKNode] {
        node.touchesCancelled(touches, withEvent: event)
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesEnded(touches, withEvent: event)
    for node in nodesTouched as! [SKNode] {
        node.touchesEnded(touches, withEvent: event)
    }
}

请注意,这是针对单次触摸的,不能很好地处理错误,如果您想正确执行此操作,则需要在 if 语句中包装一些强制向下转换

我希望这对某人有所帮助,即使这个问题已有数月之久。

关于ios - SpriteKit 自定义 UIScrollView 加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27488393/

相关文章:

ios + Parse.com - 使表只读

ios - iPhone 识别按下主页按钮

ios - IOS中带有左右箭头的UIScrollView

swift - SKAction.moveByX 没有设置 physicsBody.velocity

ios7 - Sprite Kit - 将两个物理体添加到一个 SKNode

ios - 是否有可以使用自定义 URL 调用的 iOS 应用程序列表?

ios - SwiftUI - 在 iPadOS 和 Catalyst 上使用 onTapGesture 和鼠标/触控板出现意外行为

ios - 如何在 Flutter 中实现可滚动的 Canvas ?

ios - 通过点击 UIButton 重新加载 UIScrollView

swift - 使用 SKSpriteNode 作为按钮从 GameScene 过渡到 UITabBarController