jakespracher / Snapchat-Swipe-View

Handy four way snapchat style scrolling navigation
MIT License
143 stars 19 forks source link

viewDidLoad only run once #2

Open ghost opened 8 years ago

ghost commented 8 years ago

@jakespracher I noticed that the viewDidLoad only run once, even if I swipe to open the same view controller multiple times.. Why is that?

jakespracher commented 8 years ago

All of the scroll views here are instantiated and rendered at the same time even if the others are out of view. If you open the view debugger and click the button to show clipped view you will see this. Because of this, viewDidLoad will only be called once. This is very difficult to avoid with this type of UI

ghost commented 8 years ago

@jakespracher Hm, I see.. Any ideas how I can run code every time one of the view controllers shows up?

devJoshLopez commented 8 years ago

I love this but I am having a really hard time trying to figure out how to run a function when the viewcontroller is seen. Has anyone figured this out yet? Even viewwillappear i being called when the app starts and doesnt fire again.

otymartin commented 8 years ago

@devJoshLopez Did you figure it out? Almost every tutorial or library with this navigation style omits addressing this key tradeoff.

jakespracher commented 8 years ago

Hmm, for whatever reason I didn't need this type of thing for the app that inspired this. I'll try to work on this if I have time

zalsaeed commented 7 years ago

Responding to the question of how one could know which view is being loaded at the moment? I think this could only be found by monitoring the scrollView.contentOffset for x and y axis. I haven't implemented this, but I think it is possible since we know the Offset for each view (middle, top, bottom, left and right) and they will never change. The only thing that I haven't checked is where exactly should such check/monitoring be placed. My initial thought is that the method scrollViewWillBeginDragging would be the place, but not sure. I hope this helps.

otymartin commented 7 years ago

@zalsaeed the point is that we need those viewControllerLifeCycle methods eg viewDidLoad, viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear to write custom code and execute custom behaviour on a specific viewController before and after it loads.

zalsaeed commented 7 years ago

@otymartin I think I understand the goal, but since the way this idea (Snapchat like navigation) is implemented doesn't allow the usage of those handy methods I was trying to find a walk around. The main goal is to know which view is shown at the moment (for me I want to trigger some methods as soon as a view is active), using the coordinates value is a possible solution in my opinion to that specific goal. The solution I'm proposing is definitely not functionally as good as the provided viewControllerLifeCycle methods since there is no way to capture the transition stages (e.g. willDisappear and willAppear). Please note that I'm new to iOS/Swift application, so I'm not claiming that this is the most elegant solution, but I can't think of another one at the moment. Have you found any way to know which view is being loaded even if it doesn't use the viewControllerLifeCycle methods?

otymartin commented 7 years ago

@zalsaeed somehow EZSwipeController managed to do just that. perhaps thier implementation can be looked at. its what i ended up using

petard commented 7 years ago

if added this as a work-around to notify views when they appear:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // tell child views they have appeared / disappeared
        for vc in views {
            if scrollView.bounds.intersection(vc.view.frame) == vc.view.frame {
                vc.viewDidAppear(false)
                previousVC?.viewDidDisappear(false)
                previousVC = vc
            }
        }
    }