Custom flip animation segue - Swift 3 - ios

My goal is to perform a flip-animation from view1 to view2 and vice versa.
I only want to flip the views not the whole viewController
Set up as follows:
i have a view controller (fromViewController) in my storyboard embedded in a navigationController. I drew a segue to a second view controller (toViewController) and made it a custom segue.
Now i want to add a flip animation to flip the views of the view controllers from left to right and right to left.
The forward animation already works (from fromViewController to toViewController) but he backward animation does not work.
I checked a lot of similar questions here but i couldn't find an answer that fit my needs.
I hope someone can help me out of this.
The class for the custom segue is FlipSegue.
here is my code:
import UIKit
extension UIViewController{
var isVisible: Bool{
return self.isViewLoaded && self.view.window != nil
}
}
class FlipSegue: UIStoryboardSegue {
override func perform() {
let fromViewController = self.source
let toViewController = self.destination
if fromViewController.isVisible{
UIView.transition(from: fromViewController.view, to: toViewController.view, duration: 1, options: .transitionFlipFromLeft, completion: nil)
}
else{
UIView.transition(from: toViewController.view, to: fromViewController.view, duration: 1, options: .transitionFlipFromRight, completion: nil)
}
}
}
FYI: The toViewController is not part of the NavigationController.
EDIT: I perform the segue by tapping on a UIBarButton on the left side of the navigation controller. The Button is calling the perform function in the FlipSegue class. Maybe the problem is that the toViewController is not part of the navigation stack? So i added it to the navigation stack and then i got a back button and i could return to the fromViewController with the standard animation, but this is not what i wanted.

I haven't tried this, but you should probably use from:fromViewcontroller.view in both cases, since in a back segue, from is still the starting VC.

Related

Basic problem with Custom transition using Navigation controller

In my work on an iOS multi-screen App, I am trying to get my first custom animated transaction to work. Instead of using standard animations when presenting a screen modal and fullscreen, I want to have a horizontal slide in animation.
I have made this work by overriding perform in UIStoryboardSegue, and make the animation and transaction be performed from here.
Overall, it works, except from one case, which is when a navigation controller embeds a view.
In that case, I have followed a tutorial showing precisely how custom transaction is achieved by implementing the UINavigationControllerDelegate protocol for the particular navigation controller.
import Foundation
import UIKit
class AnimatedNavigationController: UINavigationController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
func navigationController(_ navigationController: UINavigationController,
animationControllerFor operation: UINavigationController.Operation,
from fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return NavigationControllerTransition()
}
}
Unfortunately the delegate, which should return an animation object:
navigationController(_:animationControllerFor:from:to:)
is never called, and that is my primary problem.
In the tutorial mentioned, a button is used to perform a call to the navigation controller that pushes the destination view with animation set to true:
navigationController.pushViewController(view, animated:true)
In my case, I suspect that the problem is that the navigation controller pushes the view without having the animation parameter set to true, and that explains why the transaction delegate is never called.
(a confirmation off this theory will of course help a bit).
Consequently, I have tried to implement a custom segue that presents the view by calling the push function with animation set to true.
Sadly, I have not been able to get this to work.
When the navigationController.pushViewController(view, animated:true) is called, it raises an exception saying that view has already been pushed.
I have tried a lot without any luck. Code looks like this:
class NavigationControllerSegue: UIStoryboardSegue {
override func perform() {
let navigationController = self.destination as! UINavigationController
let view = navigationController.viewControllers.first!
navigationController.pushViewController(view, animated: true)
}
}
Hey first of all we are talking about two different animations... the first "UIStoryboardSegue" creates a custom subclass UIStoryboardSegue, and this is assigned to the following in question set an id to the following, below just call from the 'ViewController' a self.performSegue(withIdentifier: "YOUR-ID", sender: nil), Example of the subclass UIStoryboardSegue:
import UIKit
// Example =>
class ScaleSegue: UIStoryboardSegue {
override func perform() {
self.scale()
}
func scale() {
let toViewController = self.destination
let fromViewController = self.source
let containerView = fromViewController.view.superview
let originalCenter = fromViewController.view.center
//let originalCenter = self.posizioneClick
toViewController.view.transform = CGAffineTransform.init(scaleX: 0.05, y: 0.05)
toViewController.view.center = originalCenter
containerView?.addSubview(toViewController.view)
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
toViewController.view.transform = CGAffineTransform.identity
}, completion: { success in
fromViewController.present(toViewController, animated: false, completion: nil)
})
}
}
Now, you have to create a segue from storyboard and set as custom class of the segue from the AttributeInspector:
Now to use this you just need to call from the view controller => self.performSegue(withIdentifier: "segue1", sender: nil). Different is the way to implement Transition with UIViewControllerTransitioningDelegate.
If you need to let me know, I can give you examples using this technique too, so long.

How to Push a ViewController with a ModalView?

How can I push a UIViewController (VC) presenting a modal view (MV) to screen with a single left-to-right transition animation?
I have tried:
Setting the modal transition style of MV, and then adding both controllers to the viewControllers of the navigation controller. This however results in MV just being a controller dismissed as any other controller in the stack.
I have tried presenting MV from VC with no animation, then adding VC to the viewControllers and presenting the stack as above. This resulted in MV being instantly presented when the transition to VC starts, while VC itself animates to screen as expected. E.g. the MV doesn't follow VC when it slides in.
I have tried presenting MV from VC with animation enabled, but that results in two transitions: first VC animates to screen, then VC slides up.
I'm out of ideas, yet I would like a native and clean solution: How to transition to VC with a single normal left-to-right push, when VC is entirely covered by MV?
Key point is that MV animates together with VC; MV would appear and behave like a full screen subview, but when dismissed it animates off screen like any other modal view controller.
There are many solutions to this issue. I think UIPresentationController can provide an important clue here.
I try to make the answer as simple as possible here. You may change parameters or even subclass UIPresentationController to achieve a full animation as you wish.
import UIKit
//Green
class TransViewController: UIViewController {
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
let dest = segue.destination
let nextVC = storyboard?.instantiateViewController(withIdentifier: "modelViewController")
if let myPresenter = dest.presentationController{
myPresenter.presentedView!.addSubview(nextVC!.view)
nextVC!.view.center = CGPoint.init(x: nextVC!.view.center.x + nextVC!.view.frame.width , y: nextVC!.view.center.y)
UIView.animate(withDuration: 0.5, animations: {
nextVC!.view.center = self.view.center
}) { (success) in
dest.present(nextVC!, animated: false, completion: nil)
}
}
}
}
//Yellow ; "modelViewController"
class ModelViewController: UIViewController {
#IBAction func click(_ sender : UIButton){
self.dismiss(animated: true, completion: nil)
}
}

IOS Tab Bar Sliding

I'm trying to make tab bar with sliding (or may be swipe is right word?) effect of changing ViewControllers.
I create two ViewControllers with TableView on whole screen, but with restrictions - top edge of table not overlap top layout guide.
I link this ViewControllers with TabBarController and when I use default animation - it's OK, work fine. But I want sliding animation and do something like (swift3):
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
animateSliding(fromController: selectedViewController, toController: viewController)
return true
}
func animateSliding(fromController: UIViewController?, toController: UIViewController?) {
let fromView: UIView = fromController!.view;
let toView: UIView = toController!.view;
fromView.superview?.addSubview(toView);
toView.frame.origin.x = UIScreen.main.bounds.size.width;
UIView.animate(withDuration: 0.3,
animations: {
toView.frame.origin.x = 0;
fromView.frame.origin.x -= UIScreen.main.bounds.size.width;
},
completion: nil);
}
(it's not complete animation, just sample)
Now I have animation I wanted but second's ViewController's table overlap top guide when appear (when slide over first viewController). If I change position ViewControllers in TabBar (first became second and second became first) situation change - not first controller's table overlap top guide (when appear)
Have you tried XLPagerTabStrip? I believe that it is pretty similar to what you want to achive.
If not, you can base your code in that implementation.

Custom segue, scaling out the source view controller

Attempting to write a custom segue where the source view is scaled out, while changing the alpha of the destination to fade the destination in. The destination is a MKMapView, so I want it updating as the fade occurs.
With what I've tried I end up with the source and designation scaling out simultaneously, and I can't get just the source view to scale out.
class Map_Segue: UIStoryboardSegue {
override func perform()
{
var sourceViewController : UIViewController = self.sourceViewController as UIViewController
var destinationViewController : UIViewController = self.destinationViewController as UIViewController
destinationViewController.view.alpha = 0 sourceViewController.addChildViewController(destinationViewController) sourceViewController.view.addSubview(destinationViewController.view)
UIView.animateWithDuration(2.0,delay:1.0,options: UIViewAnimationOptions.CurveEaseInOut, // delay of 1 second for aesthetics
animations:
{
sourceViewController.view.transform = CGAffineTransformScale(sourceViewController.view.transform, 100.0, 100.0);
destinationViewController.view.alpha = 1;
},
completion:
{ (finished:Bool)->Void in
destinationViewController.didMoveToParentViewController(sourceViewController);
}
)
}
}
I've tried autoresizesSubviews=false, but that doesn't seem to do anything.
I've tried setting the destination transform in the animation to be 1/100 (and set the options to UIViewAnimationOptions.CurveLinear which has the final result correct, but the transition effect is wrong (map in the background scaled up then down again)
I'm sure this should be easy, and I'm missing a trick as I'm new to this.
Anyone got any ideas?
Update:
I've found that (somewhat obviously) I should use sourceViewController.view.superview?.insertSubview( destinationViewController.view, atIndex:0) to insert it alongside the original source view, rather than as a child of it, that way, obviously, the transform is independent, not with respect to the views parent (which it will be as a subview). The problem then is swapping to the new view. Using the method I had, viewWillAppear and similar are not called, and the changing over the views does not work. If I call presentViewController, then we get a glitch when viewWillAppear is called.
Solution so far
Forget using custom segues. Followed the suggestion and placed a UIImageView on top of the map view, and had a beautiful animating fade in about 5 minutes of coding.
I think you are a bit confused with parent and child view controllers etc. It is sufficient to temporarily add the second view controller's view to the first one, perform the transitions and then clean up.
I tested the following in a simple sample project. Note that I am adding the second view controller's view to the window rather than the first view controller's view because otherwise it would also get scaled up.
override func perform() {
let first = self.sourceViewController as ViewController
let second = self.destinationViewController as ViewController
second.view.alpha = 0
first.view.window!.addSubview(second.view)
UIView.animateWithDuration(2.0, delay: 0.0,
options: .CurveEaseInOut, animations: { () -> Void in
first.view.transform = CGAffineTransformMakeScale(100.0, 100.0)
second.view.alpha = 1.0
})
{ (finished: Bool) -> Void in
second.view.removeFromSuperview()
first.presentViewController(second, animated: false, completion: nil)
}
}

Presented view controller disappears after animation using custom UIViewController animations

I've looked around for an answer for this and spent the last two hours pulling my hair out to no end.
I'm implementing a very basic custom view controller transition animation, which simply zooms in on the presenting view controller and grows in the presented view controller. It adds a fade effect (0 to 1 alpha and visa versa).
It works fine when presenting the view controller, however when dismissing, it brings the presenting view controller back in all the way to fill the screen, but then it inexplicably disappears. I'm not doing anything after these animations to alter the alpha or the hidden values, it's pretty much a fresh project. I've been developing iOS applications for 3 years so I suspect this may be a bug, unless someone can find out where I'm going wrong.
class FadeAndGrowAnimationController : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController!, presentingController presenting: UIViewController!, sourceController source: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
return self
}
func animationControllerForDismissedController(dismissed: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
return self
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {
return 2
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as UIViewController
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as UIViewController
toViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
toViewController.view.alpha = 0
transitionContext.containerView().addSubview(fromViewController.view)
transitionContext.containerView().addSubview(toViewController.view)
transitionContext.containerView().bringSubviewToFront(toViewController.view)
UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
fromViewController.view.transform = CGAffineTransformScale(fromViewController.view.transform, 2, 2)
fromViewController.view.alpha = 1
toViewController.view.transform = CGAffineTransformMakeScale(1, 1)
toViewController.view.alpha = 1
}, completion: { finished in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
})
}
}
And the code to present:
let targetViewController = self.storyboard.instantiateViewControllerWithIdentifier("Level1ViewController") as Level1ViewController
let td = FadeAndGrowAnimationController()
targetViewController.transitioningDelegate = td
targetViewController.modalPresentationStyle = .Custom
self.presentViewController(targetViewController, animated: true, completion: nil)
As you can see, a fairly basic animation. Am I missing something here? Like I said, it presents perfectly fine, then dismisses 99.99% perfectly fine, yet the view controller underneath after the dismissal is inexplicably removed. The iPad shows a blank screen - totally black - after this happens.
This seems to be an iOS8 bug. I found a solution but it is ghetto. After the transition when a view should be on-screen but isn't, it needs to be added back to the window like this:
BOOL canceled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!canceled];
if (!canceled)
{
[[UIApplication sharedApplication].keyWindow addSubview: toViewController.view];
}
You might need to play around with which view you add back to the window, whether to do it in canceled or !canceled, and perhaps making sure to only do it on dismissal and not presentation.
Sources: Container view disappearing on completeTransition:
http://joystate.wordpress.com/2014/09/02/ios8-and-custom-uiviewcontrollers-transitions/
I was having the same problem when dismissing a content view controller. My app has this parent view controller showing a child view controller. then when a subview in the child is tapped, it shows another vc (which I am calling the content vc)
My problem is that, when dismissing the contentVC, it should go to child VC but as soon as my custom transition finishes, childVC suddenly disappears, showing the parent VC instead.
What I did to solve this issue is to
change the .modalPresentationStyle of the childVC presented by parentVC from the default .automatic to .fullscreen.
Then changed the .modalPresentationStyle of contentVC to .fullscreen as well.
This solves the issue. but it won't show your child VC as a card sheet (when using .overCurrentContext or automatic) which is new in iOS 13.
had the same problem ios 8.1
my swift code after completeTransition
if let window = UIApplication.sharedApplication().keyWindow {
if let viewController = window.rootViewController {
window.addSubview(viewController.view)
}
}

Resources