Showing ViewController's View when animating upper ViewController - swift

I have ViewController(1) presented over another ViewController(2), I want to accomplish the effect that when I dismiss viewController1 I perform a reduction of size through CGAffineTransform and I dismiss it.
The effect I want to accomplish though is that when I reduce the size of ViewController1's view I want to see also the view of ViewController2 behind it, while now I'm only seeing a black background.
The code I'm using is really simple:
UIView.animate(withDuration: 2, animations: {
self.view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}) { (_) in
self.dismiss(animated: true, completion: nil)
}
I don't know how to reach that effect

Make sure to set this presentation for the top animated vc
vc.modalPresentationStyle = .overCurrentContext
This will guarantee transparency behind when you change frame / transform the view of that top vc

Related

Is it possible to use swipe to dismiss while presenting a fullscreen modal in iOS 13?

With the new default card modal presentation in iOS 13, there is a nice feature that is swipe to dismiss. Is it possible to use this feature while presenting a classic .fullscreen modal?
I checked and if isModalInPresentation is false while presenting in fullscreen.
Any ideas?
It seems that the swipe to dismiss will only work if the modal is presented as a sheet, as stated in this year's wwdc:
Now, what do you all have to do to support Pull to Dismiss? In general, nothing. If you present something as a Sheet, the ability to pull it down comes for free.
And it makes sense. When you present it as a sheet, the UI makes it look like you can swipe the modal down. When you present it on fullscreen, it would not be intuitive for the user that he should swipe the page down to dismiss. I'd rather use a button on this case.
As #pepsy said, the full screen view is not intended to be dismissed by swiping. However, if you still want to try it, here's something I started
NOTE: it's a bit glitchy the parent controller is not visible while dragging.
I added a UIPanGestureRecognizer to the view to simulate the swipe to dismiss behavior. I also have a Close (X) button, as the swipe gesture is not as intuitive as for a sheet.
#IBAction func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {
let translationY = sender.translation(in: sender.view!).y
switch sender.state {
case .began:
break
case .changed:
view.transform = CGAffineTransform(translationX: 0, y: translationY)
case .ended, .cancelled:
if translationY > 160 {
dismiss(animated: true, completion: nil)
} else {
UIView.animate(withDuration: 0.2, animations: {
self.view.transform = CGAffineTransform(translationX: 0, y: 0)
})
}
case .failed, .possible:
break
#unknown default:
break
}
}

Segue transitions to the wrong page Swift Xcode

I am trying to create a series of pages that work in tandem using custom segues. These segues work fine until I have them execute in a certain series. From VC1 I go to VC2 via the default modal segue (it pops up over the original segue). Then, from VC2 I go to VC3 using a custom horizontal segue (code below). Finally, I go back from VC3 to VC1 using a custom unwind horizontal segue. The problem is that when I go back to VC1, VC2 appears instead. I checked and ViewDidLoad does not execute in VC2 when it appears, but I can still interact with it when triggered. My best guess as to what is happening is that VC2 covers VC1 so when I go back to VC1, VC2 is displayed on top of it. Even if this is the problem, I don't know how to fix it. Code below:
Horizontal Segue:
class HorizontalSegue: UIStoryboardSegue {
override func perform() {
let src = self.source as UIViewController
let dst = self.destination as UIViewController
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseOut], animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
},
completion: { finished in
src.present(dst, animated: false, completion: nil)
})
}
}
Unwind Horizontal Segue:
class UnwindHorizontalSegue: UIStoryboardSegue {
override func perform() {
let src = self.source as UIViewController
let dst = self.destination as UIViewController
src.view.superview?.insertSubview(dst.view, belowSubview: src.view)
src.view.transform = CGAffineTransform(translationX: 0, y: 0)
UIView.animate(withDuration: 0.2, delay: 0.0, options: [.curveEaseIn], animations: {
src.view.transform = CGAffineTransform(translationX: src.view.frame.size.width, y: 0)
},
completion: { finished in
src.dismiss(animated: false, completion: nil)
})
}
}
The first thing I noticed is that you said ViewDidLoad is not called for VC2. Please see if ViewDidAppear (or will appear) is being called instead.
ViewDidLoad is only called when the view loads (initially, when it wasn't there before). ViewDidAppear should fire always, when the view is brought into the user's screen.
After that, log dst in your custom UIStoryBoardSegue. If that's an instance of VC2, you are simply pushing the wrong view controller.
I also see you're using src.present and src.dismiss in your UIStoryboardSegue. This means you're not actually pushing views, but presenting them "on top" of an active view controller. Try to rethink that logic, since this is very much likely where the problem lies.
I would rather try and push the view controllers (normally, instead of 'presenting' them) and change the UIStoryboardSegue appearance to fake the animation if that's what you're after.

Transition to a new ViewController embedded in an UINavigationController causes animation problem

I use a rootViewController and I want to move to another ViewController. The transition to the newViewController works with that code.
A problem occurs when the newViewController is embedded in an UINavigationController. Then the navigation bar is animating during the animation and changes the position.
The navigation bar is animating from the top left to the correct position.
fileprivate func animateTransition(to newViewController: UIViewController) {
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
transition(from: currentViewController, to: newViewController, duration: 2, options: [.transitionCrossDissolve, .curveEaseOut], animations: {
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
}, completion: nil)
}
How is it possible to move to another UINavigationController with a "fade" animation and how can the navigation bar be at the correct position right from the beginning of the animation?
First, you should move your view controller clean-up code call from the animations closure into the completion closure:
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
transition(from: currentViewController, to: newViewController, duration: 2, options: [.transitionCrossDissolve, .curveEaseOut], animations: {
// this is intentionally blank
}, completion: { _ in
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
})
You don't want to indicate the transition is done until the animation is complete.
To the navigation bar issue, rather than letting transition(from:to:duration:...) handle the manipulation of the view controller hierarchy, you can add it to your view hierarchy and then animate the unhiding of it. Usually you'd use the .showHideTransitionViews option, but transition is still doing something curious with the appearance methods that is confusing the navigation controller, so it's best to just animate it yourself:
currentViewController.willMove(toParent: nil)
addChild(newViewController)
newViewController.view.frame = view.bounds
newViewController.view.alpha = 0
view.addSubview(newViewController.view)
UIView.animate(withDuration: 2, delay: 0, options: .curveEaseOut, animations: {
newViewController.view.alpha = 1
}, completion: { _ in
self.currentViewController.view.removeFromSuperview()
self.currentViewController.removeFromParent()
newViewController.didMove(toParent: self)
self.currentViewController = newViewController
})
That will allow it to present the navigation bar correctly from the beginning and just fade it in.
try putting this underneath the class declaration of the newViewController
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
if you already have a viewDidLoad() in your ViewController, then just use the last part.
If that doesn't work let me know.

How to open another view on animation completition in swift?

I am trying to design animated splash screen.
I created the the splash screen and added a segue to my first screen, but I am not sure how I can start the first screen when the animation is finished.
UIView.animate(withDuration: 0.5, animations: {
// your animation
}) { (completed) in
let yourController = YourController()
self.navigationController?.pushViewController(yourController, animated: true)
}

How to modify slide transition

I had asked a question similar to this once before but I didn't realize and don't think I can use the same method due to the nuances of the two methods. (How to stop slide transition into Second VC before it covers UIView?)
I ultimately learned how to do the transition using APPCoda's lesson(http://www.appcoda.com/custom-segue-animations/)
The original question resulted in an answer providing a solution using container views and hard coded views. What I am wondering is if I can get the same effect using two separate view controllers and linking them through a segue with a gesture recognizer.
What I would like to accomplish is:
Have my initial view controller
Tap Button and have Second View Controller Overlap the first View Controller Partially (By partially I mean I have a UIView on the first View Controller that I want to remain visible. So the top of the second view controller will slide up until it hits the bottom of the UIView).
What I currently have is the original view controller being pushed up and out of the screen by the second view controller sliding up from the bottom of the screen.
Code that handles the transition using a segue from one VC to the sliding VC:
import Foundation
class CustomSegueToSecondVC: UIStoryboardSegue
{
override func perform() {
let originalVC = self.sourceViewController .view as UIView!
let slidingVC = self.destinationViewController.view as UIView!
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
slidingVC.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)
let window = UIApplication.sharedApplication().keyWindow
window?.insertSubview(slidingVC, aboveSubview: originalVC)
UIView.animateWithDuration(0.4, animations: { () -> Void in
originalVC.frame = CGRectOffset(originalVC.frame, 0.0, -screenHeight)
slidingVC.frame = CGRectOffset(slidingVC.frame, 0.0, -screenHeight)
}) { (Finished) -> Void in
self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
}
}
};