UIScrollView不向上/向下滚动所有方式

时间:2015-07-31 05:42:20

标签: ios swift uiscrollview sprite-kit swift2

我目前正在使用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具有负高度,但它根本无法滚动。

我刚刚发现视图大小不足。

2 个答案:

答案 0 :(得分:2)

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

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

在我们创建的MainScene中:

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

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

然后,在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,则应在background方法中移动update:

答案 1 :(得分:0)

我发现视图框架的大小会缩短。我通过简单地添加一个插入视图框架大小的插图来解决这个问题,因此它更进一步。