Reputation: 1708
I am currently using SpriteKit
+ UIScrollView
+ SKCameraNode
to create a scrolling game. I am currently trying to reverse the direction of the UIScrollView
in the vertical direction, as the scroll view seems to do it backwards from what is intuitive.
My solution to this was to put the contentOffset
from the start for the scroll view to the max height, and set the camera's position to the y position in the other direction.
However, the scrolling stops halfway down at (0, 320) when it should go to (0, 0).
Here is the code for the scroll view:
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)
Here is the code when the scroll view scrolls:
let contentOffset = CGPointMake(scrollView.contentOffset.x, self.height - scrollView.contentOffset.y)
self.gScene.setContentOffset(contentOffset)
And here is the code for setting the content offset:
self.camera!.position = CGPointMake(ogPos.x + contentOffset.x, ogPos.y + contentOffset.y)
So the camera works and scrolls in the correct direction now; however, the scrolling's content offset gets stuck at (0, 320) for no reason, and it refuses to scroll further down. If you have any ideas I'd love to hear them! Thanks!
EDIT: I also tried making the UIScrollView's contentSize with a negative height, but then it cannot scroll at all.
I just discovered that it stops short by the size of the view.
Upvotes: 0
Views: 776
Reputation: 1708
I discovered that it was stopping short by the size of the view's frame. I fixed the issue by simply adding an inset the size of the view's frame so it goes that much farther.
Upvotes: 0
Reputation: 2401
It is not a good idea to mix UIKit
and SpriteKit
a lot, because they work differently (different positions, different units, etc).
If you want to drag the MainScene
around, you can do this:
In our MainScene
we create:
let background:SKSpriteNode = SKSpriteNode(imageNamed: "background")
var lastTouch : CGPoint?
We will add background to our scene, and every SKNode
as a child to this background (platforms, the player, or whatever you want to show).
Then, in touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent)
, we want to get the user touch and move this background depending in the user touch movement. A basic implementation would be:
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;
}
If you wanted to move automatically the MainScene
, you should move background
in the update:
method.
Upvotes: 2