UIScrollView 没有一直滚动 Up/Down

UIScrollView Not Scrolling All The Way Up/Down

我目前正在使用 SpriteKit + UIScrollView + SKCameraNode 创建一个卷轴游戏。我目前正在尝试在垂直方向上反转 UIScrollView 的方向,因为滚动视图似乎与直观的方向相反。

我的解决方案是将 contentOffset 从滚动视图的开始设置到最大高度,并将相机的位置设置为另一个方向的 y 位置。

但是,当滚动应该转到 (0, 0) 时,滚动在中途停止在 (0, 320)。

滚动视图的代码如下:

let contentSize = CGSize(width: 1575, height: 760) //adjust depending on level
self.height = contentSize.height
scrollView.contentSize = contentSize
scrollView.contentOffset = CGPointMake(0, contentSize.height / 2)

滚动视图滚动时的代码如下:

    let contentOffset = CGPointMake(scrollView.contentOffset.x, self.height - scrollView.contentOffset.y)
    self.gScene.setContentOffset(contentOffset)

这里是设置内容偏移量的代码:

self.camera!.position = CGPointMake(ogPos.x + contentOffset.x, ogPos.y + contentOffset.y)

现在相机可以正常工作并以正确的方向滚动;然而,滚动的内容偏移无缘无故地卡在 (0, 320),并且它拒绝进一步向下滚动。如果您有任何想法,我很想听听!谢谢!

编辑:我还尝试将 UIScrollView 的 contentSize 设置为负高度,但它根本无法滚动。

我刚刚发现它会因视图的大小而停止。

混合使用 UIKitSpriteKit 不是一个好主意,因为它们的工作方式不同(不同的位置、不同的单位等)。

如果你想拖动MainScene,你可以这样做:

在我们的 MainScene 中,我们创建:

let background:SKSpriteNode = SKSpriteNode(imageNamed: "background")
var lastTouch : CGPoint?

我们将为我们的场景添加背景,并且每个 SKNode 作为此背景的 child(平台、播放器或任何您想要显示的内容)。

然后,在 touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) 中,我们希望获得用户触摸并根据用户触摸移动来移动此背景。一个基本的实现是:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    var touch : UITouch = touches.first as! UITouch
    lastTouch = touch .locationInNode(self)
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch moves */
    var touch : UITouch = touches.first as! UITouch
    var touchLocation : CGPoint = touch .locationInNode(self)

    //We uptade the background position
    background.position = CGPointMake(background.position.x + (touchLocation.x - lastTouch!.x), background.position.y + (touchLocation.y - lastTouch!.y))

    //We update the lastTouch
    lastTouch = touchLocation;
}

如果您想自动移动 MainScene,您应该在 update: 方法中移动 background

我发现它因视图框架的大小而停止。我通过简单地添加一个视图框架大小的插图来解决这个问题,这样它就可以走得更远。