crashoverride777 / swifty-sk-scroll-view

A Swift library to add a UIScrollView to your SpriteKit scenes. This library is deprecated and no longer supported.
MIT License
60 stars 11 forks source link

present it again? #2

Closed mshll closed 8 years ago

mshll commented 8 years ago

Hello.. well I've been using this for a while and it's awesome but there is one issue whenever I move to the main scene and access it again it shows me where I was when I hit the back button but whenever I swipe it take me to the top..... is there a way to make it shows from the top whenever I access it again?

crashoverride777 commented 8 years ago

Hey,

Thank you very much for using my helper. I am not sure why it is not resetting but you can reset the scroll view manually using the contentOffset property.

So if you want to make sure you always start at the top (vertical scroll view) you can add this line right after you added the scrollView to the scene

    view?.addSubview(scrollView)
    scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

For example if you would like to start on the 2nd page than you would say this

    scrollView.setContentOffset(CGPoint(x: 0, y: 0 + scrollView.frame.size.height), animated: true)

Hope this helps and let me know how it goes.

mshll commented 8 years ago

Thanks so much! , and yeah I know I reported it :b but thanks !!

crashoverride777 commented 8 years ago

Oh its you haha I didnt realise. Happy coding and thanks again

mshll commented 8 years ago

Well I did it but it doesn't work no idea why.... and tried to add it to DidMovetoView but I get BAD_EXC

anyway I tried what you said but nothing here is the code:

let selectPlayerScene:SKScene = SelectPlayerScene(size: self.size)
                scene!.view?.presentScene(selectPlayerScene, transition: transition)
                scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
crashoverride777 commented 8 years ago

I am confused. Are you not removing the scrollView before transiting to the new scene?

crashoverride777 commented 8 years ago

You cannot do this

  class Scene1: SKScene {

    weak var scrollView:........
 }

and than transition to a new scene (which does not remove the scrollView because its added to the view and no the scene) and try to do something with the scrollView in the new scene

mshll commented 8 years ago

Well I use this is the code when I want to present the scene where I want to use the scroll view:

                CustomScrollView.enable()
                let selectPlayerScene:SKScene = SelectPlayerScene(size: self.size)
                scene!.view?.presentScene(selectPlayerScene, transition: transition)
                scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

And this is when I get back to the main scene:

    func MovetoScene(){
        scene?.removeAllChildren()
        scrollView.removeFromSuperview()
        CustomScrollView.disable()
        let gameScene:SKScene = GameScene(size: self.size)
        scene!.view?.presentScene(gameScene, transition: transition)

    }
mshll commented 8 years ago

Never mind I figured it out I edited the movetoscene func and IT WORKED!

thanks! code:

    func MovetoScene(){
        scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
        scene?.removeAllChildren()
        scrollView.removeFromSuperview()
        CustomScrollView.disable()
        let gameScene:SKScene = GameScene(size: self.size)
        scene!.view?.presentScene(gameScene, transition: transition)

    }
crashoverride777 commented 8 years ago

Cool you should still try to keep it simple like this. You should present the scrollView in 1 scene where you want to use it, set it Up in ViewDidLoad

    class SceneIWantScrollView: SKScene {

     weak var scrollView....

     override func didMoveToView(view: SKView) {
          //  setUpScrollView code
    }

than in the code where you are transitioning to a new scene you should remove it

    scrollView?.removeFromSuperView()

and thats all you have to do.

Now when you transition back to

           class SceneIWantScrollView...

it will add the scroll view again.

Not sure what you are trying do do exactly.

Keep me posted

mshll commented 8 years ago

i don't really follow you on the weak var thing :b I tried to type it but it pop up an error..

crashoverride777 commented 8 years ago

You should read the instructions on gitHub, I am not sure whats confusing you, weak just means it doesnt hold a strong reference to the ScrollViewClass. You scene with the scrollView should look like this.

    class SceneIWantScrollView: SKScene {

        weak var scrollView: CustomScrollView!

 override func didMoveToView(view: SKView) {

    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.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height * 3)
    view?.addSubview(scrollView)
}

Than in the same scene in the method where you are transitioning to a new scene you should remove it

      func moveToNewScene() {
           scrollView?.removeFromSuperView()
    }

and thats it.

Later on when you transition back to the scrollView scene it will just reAdd it again.

mshll commented 8 years ago

Well I have the weak above the class not inside it and I have those codes in my viewdidload ...

to make things clear: I have 2 Scenes... one is a scrollable menu where you select/change your player

and the second is the game scene and I have buttons to transition here and there in both scenes...

I tried just removefromsuperview but it just disable the scrollview but still in the shop and when I transition I should remove the childs because I'll crash if I moved to it again....

crashoverride777 commented 8 years ago

Why is you scrollView weak property above the class? It makes no sense because there is no need to make it global to all files in your project.

Put the scrollView weak property in the scene with the player select screen. Than remove the scrollView in the method in that class where you are transitioning to the gameScene.

You dont have to do anything in your gameScene with the scrollView.

In the sample project on github I am changing between 2 scenes exactly like you want. Maybe have a look again at the GameScene.swift file on how to do it.

Dont hesitate to keep asking me, I am more than happy to help.

mshll commented 8 years ago

alright I did what you said.... and in the Transition func I just did present scene code.. but when I go to the main scene I can't touch because there is still scroll

mshll commented 8 years ago

Nvm I checked the updated sample and it worked thanks! it's less complicated and less codes now :b

crashoverride777 commented 8 years ago

Awesome

mshll commented 8 years ago

thanks, you helped me a lot! and sorry for my lack of knowledge it's because I'm new to the whole coding thing it's the first language I learned (Swift) and it's been just 3-5months... and this is my first real project :b

crashoverride777 commented 8 years ago

No worries, I am quite new to coding as well. Happy coding