1

In my UIStoryboardSegue subclass, I use presentViewController at the end of the perform method, this cause viewDidAppear/viewWillAppear to be called twice?

How can I prevent this?

Thanks

Current code:

override func perform() {

    // Assign the source and destination views to local variables
    let sourceView = sourceViewController.view as UIView!
    let destView = destinationViewController.view as UIView!

    // Get the screen width and height
    let screenWidth = UIScreen.mainScreen().bounds.size.width
    let screenHeight = UIScreen.mainScreen().bounds.size.height

    // Specify the initial position of the destination view
    destView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)

    // Add the destination view to the window
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(destView, aboveSubview: sourceView)

    // Animate the transition
    UIView.animateWithDuration(0.7, animations: { () -> Void in

        // Scale down the source view
        sourceView.transform = CGAffineTransformScale(sourceView.transform, 0.90, 0.90)

        }) { (Finished) -> Void in
    }

    UIView.animateWithDuration(1.0, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.7, options: .CurveEaseOut, animations: { () -> Void in

        destView.frame = CGRectOffset(destView.frame, 0.0, -screenHeight)

        }) { (finished) -> Void in

            self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
    }
}

I see now that without presentViewController the new controller is destroyed soon after the animation custom segue. I see why we need it.

Shay
  • 2,595
  • 3
  • 25
  • 35

1 Answers1

0

The reason why viewDidAppear gets called twice is that you first insert the viewcontroller as a subview into the keyWindow. (which triggers the viewDidAppear) enter image description here

and then present the viewcontroller. (which triggers the viewDidAppear also) enter image description here

This brings also some side effects to your app because you will end up with a viewHierachy like:

-- KeyWindow
---- FirstVC View
---- SecondVC View
---- UITransitionView (because of the presentVC)
------- SecondVC View

If you want to achieve this swipe/scroll transition why not just use a scrollView and have childviewcontrollers in it?

The other solution for this would be to use a custom transition and simply call presentViewController with a custom transition as described here: http://www.raywenderlich.com/113845/ios-animation-tutorial-custom-view-controller-presentation-transitions

Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51
  • OK, thanks. But why a respectable sites do this in their tutorials? They put presentViewController in the end of the perform method? – Shay Dec 27 '15 at 17:32
  • @Shay you could not be any more unspecific ^^ please tell use the site so we can check for our own. – luk2302 Dec 27 '15 at 17:32
  • can you please provide a link? Maybe I get your question wrong :) – Christian 'fuzi' Orgler Dec 27 '15 at 17:32
  • Just tried in my code with out presentViewController and without dismissViewControllerAnimated in the unwind custom segue. It crashed. We need the presentViewController for this. – Shay Dec 27 '15 at 17:42
  • Add code and important comment. Also tested appcoda code, same results. – Shay Dec 27 '15 at 17:54
  • Thanks christian, check the other animation, you can't do it with scroll view as you know. Also I have more great animation transition so, this custom segue technic is superb. Thanks for the link, I need a solid way to achieve all the animation I did using the custom segue. – Shay Dec 27 '15 at 19:18
  • I would recommend to do the first animation via a scrollView as the user also has a better experience then for this transition. For the second animation you can achieve this with a custom presentation transition, why not do so? :) – Christian 'fuzi' Orgler Dec 27 '15 at 19:27
  • Custom presentation transition? You mean using the link you provided? If yes, so, I have the book, didn't reached to that chapter yet, going to read it now. – Shay Dec 27 '15 at 19:39