52inc / Pulley

A library to imitate the iOS 10 Maps UI.
https://cocoapods.org/pods/Pulley
MIT License
2.02k stars 265 forks source link

drawerPositionDidChange is stuck in a loop #381

Closed adar2378 closed 4 years ago

adar2378 commented 4 years ago

Here is my code. It is forever stuck in a loop :C


extension PrimaryContentViewController: PulleyDrawerViewControllerDelegate {
    func collapsedDrawerHeight(bottomSafeArea: CGFloat) -> CGFloat {
        return 0.0
    }

    func partialRevealDrawerHeight(bottomSafeArea: CGFloat) -> CGFloat {
        return 0.0
    }

    func supportedDrawerPositions() -> [PulleyPosition] {
        return PulleyPosition.all
    }
    func drawerPositionDidChange(drawer: PulleyViewController, bottomSafeArea: CGFloat)
    {
        print(drawer.drawerPosition);
        var centerPoint = self.mapView.compassView.center
        centerPoint.x = 40
        self.mapView.compassView.center = centerPoint

        if self.Navview.frame.origin.y == self.view.frame.origin.y {
            if drawer.drawerPosition != .closed{
                drawer.setDrawerPosition(position: .closed, animated: false)
            }
        }

        if(drawer.drawerPosition != .open){
                 print("inside primary view controller")
            mapHelper.needToResizePointAnnotations(position: drawer.drawerPosition, mapView: self.mapView, layerView: layerView)
        }
        if drawer.drawerPosition == .collapsed && PoiDetailsFound == false{
            temperatureLabelBottomConstraint.constant = 128.0 + temperatureLabelBottomDistance
            MapLogoConstraint.constant = 128.0 + temperatureLabelBottomDistance
        }
    }

    func drawerDisplayModeDidChange(drawer: PulleyViewController) {

    }

}

extension PrimaryContentViewController: PulleyPrimaryContentControllerDelegate {

    func makeUIAdjustmentsForFullscreen(progress: CGFloat, bottomSafeArea: CGFloat)
    {
        guard let drawer = self.parent as? PulleyViewController, drawer.currentDisplayMode == .bottomDrawer else {
        //    controlsContainer.alpha = 1.0
            return
        }

      //  controlsContainer.alpha = 1.0 - progress
    }

    func drawerChangedDistanceFromBottom(drawer: PulleyViewController, distance: CGFloat, bottomSafeArea: CGFloat)
    {
        guard drawer.currentDisplayMode == .bottomDrawer else {

            temperatureLabelBottomConstraint.constant = temperatureLabelBottomDistance
            return
        }

        if distance <= 285.0 + bottomSafeArea
        {
            temperatureLabelBottomConstraint.constant = distance + temperatureLabelBottomDistance
            MapLogoConstraint.constant = distance + temperatureLabelBottomDistance

        }
        else
        {
            temperatureLabelBottomConstraint.constant = 285.0 + temperatureLabelBottomDistance
            MapLogoConstraint.constant = 285.0 + temperatureLabelBottomDistance

        }
    }

    func hexStringToUIColor (hex:String) -> UIColor {
        var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if (cString.hasPrefix("#")) {
            cString.remove(at: cString.startIndex)
        }

        if ((cString.count) != 6) {
            return UIColor.gray
        }

        var rgbValue:UInt32 = 0
        Scanner(string: cString).scanHexInt32(&rgbValue)

        return UIColor(
            red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }

}
amyleecodes commented 4 years ago

You can’t call setDrawerPosition inside of drawerPositionDidChange.

Setting the drawer position calls drawerPositionDidChange....which just sets the drawer position again and creates an infinite loop.

adar2378 commented 4 years ago

oh sorry. I am very new to iOS development. Thanks for your quick response. I removed the setDrawerPosition method call inside of drawerPositionDidChange but it is still stuck in a loop :( Could it be because of something else? Or setDrawerPositionChange is getting called from somewhere else?

amyleecodes commented 4 years ago

Anything that triggers a layout pass in Pulley could trigger it. Place a breakpoint and see what your callstack looks like. You can see what is causing it to happen.

adar2378 commented 4 years ago

Thanks. I'll let you know I find anything

adar2378 commented 4 years ago

Hi I think this where the setDrawerPosition is getting called. Inside of viewDidLayoutSubviews method "https://i.ibb.co/ynZjkbj/Screenshot-2020-03-31-at-2-50-02-PM.png"

amyleecodes commented 4 years ago

Yes, that’s fine. But, that’s initial load. You need to continue until you start seeing a recursive stack that shows it’s in an infinite loop so you can see what you’re doing to cause it to loop.

This is just initial load, and it’s fine.

adar2378 commented 4 years ago

This is what the trace stack looks like: Video Link

amyleecodes commented 4 years ago

I get a playback error.

However, there’s very limited help I can provide as this isn’t related to Pulley. I can’t provide General iOS dev help here.

adar2378 commented 4 years ago

Sorry for the inconvenience, video link Please have a look here. I'd really appreciate your feedback, thanks.

amyleecodes commented 4 years ago

The stack on the left isn’t changing, so you’re not actually stepping through anything when you’re hitting continue. It’s still just the first time load process from when you hit the button, and it’s not an infinite loop.

People usually see infinite loops when they change drawer positions / call Pulley methods in the delegates, or cause layout passes to occur in one of their view controllers. For example, someone did something in a delegate that called “parent.view.layoutIfNeeded”.

My recommendation for debugging is to empty all of the delegate methods, empty any layoutSubviews / viewDidLayoutSubviees / viewWillLayoutSubviews methods you have and see if it still happens. If not, it’s some code in one of your two view controllers. You can slowly add back bits of code and try it until you figure out the code causing your problem.

You can also test it with an empty VC for both drawer and primary content (one at a time, and then both) to narrow down which one (or both) have code in them causing the issue.

adar2378 commented 4 years ago

Thank you so much <3 As I'm very new(only learning for a week) to iOS development. The print statements were recurring so I thought maybe it is stuck in a loop with some contradictory statements.

I'll try to do what you said. Have a nice day.