Storyboard Custom-Segue tranition for dismissing a UIViewController - ios

I am trying to implement a 'Cover Horizontal' transition for my Storyboard's UIViewControllers. A simple slide right to left and back again. First I am surprised that this is not an option in Interface Builder. Doesn't everybody need this or am I missing something?
I created a Custom-Segue and added a Sliding Transition.
#import "SlideLeftCustomSegue.h"
#implementation SlideLeftCustomSegue
- (void)perform{
UIViewController *srcViewController = (UIViewController *) self.sourceViewController;
UIViewController *destViewController = (UIViewController *) self.destinationViewController;
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[srcViewController.view.window.layer addAnimation:transition forKey:nil];
[srcViewController presentViewController:destViewController animated:NO completion:nil];
}
#end
That all works fine in the push direction but how do I get the same effect for the dismiss? It all seems a bit difficult for a very simple transition. Again I feel I am missing something.....
Any help would be great.

Related

Retain SourceViewController after custom push segue

I've got a custom UIStoryboardSegue called leftToRightSegue. Every time this segue is triggered and I move back to the source view, the viewdidload method of the sourceViewController is called. This is not something I want, since the setup is quite heavy.
This is the code I'm using for the segue:
#import "LeftToRightPushSegue.h"
#import "QuartzCore/QuartzCore.h"
#implementation LeftToRightPushSegue
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
}
I am in fact popping the view back, for anyone interested:
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController popViewControllerAnimated:NO];
How do I retain the sourceViewController?
I would try to move the code to the init method if possible.
If that's not possible, perhaps create a boolean property. i.e.
#property (nonatomic, strong) BOOL *isInitialised;
- (void) viewDidLoad
{
if (!self.isInitialised)
{
...
self.isInitialised = YES;
}
}
You set it to true once the actions in the viewDidLoad have been performed. There are better patterns if you search a little. This is a bit of quick and dirty way of getting it to work.
Also keep in mind that you'll be keeping in memory stuff, not directly visible to the user.

Dropdown and bounce Segue anitimation

I am trying to replicate the following segue animation as seen in Secret where it bounces on the bottom of the screen. I have searched the internet but can't find anything to help me.
I am looking for the segue animation to dropdown from the top and bounce on the bottom of the screen.
Currently I have this but it doesn't bounce.
- (void)perform {
UIViewController *srcViewController = (UIViewController *) self.sourceViewController;
UIViewController *destViewController = (UIViewController *) self.destinationViewController;
CATransition *transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromBottom;
[srcViewController.view.window.layer addAnimation:transition forKey:nil];
[srcViewController presentViewController:destViewController animated:NO completion:nil];
}
If it helps I use the POP animation engine.
You need a custom transition using UIKit Dynamics to perform the animation.

wMemory Warning and application Crash due to poor ViewController handling?

I have a line of viewControllers set in line. The idea is to go through a process of questions etc. If i was to use the already set up way of changing view controller (through button - modal), i would have no problem with memory. I have tried it. However i need to change the views programmatically and not with buttons directly.
When i do this programmatically my memory usage just starts increasing as i go through the views, until eventually i get a Memory Warning and a crash.
To change the viewController i use custom UIStoryBoardSegue and also methods in the viewController files.
Here is how i do it:
Method in View Controller:
-(void)changeViewController
{
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:transition forKey:nil];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"enterInfoSecondViewController"];
[[[[[UIApplication sharedApplication]delegate]window]rootViewController]presentViewController:vc animated:NO completion:nil];
// [self dismissViewControllerAnimated:YES completion:nil];
//[self presentViewController:vc animated:NO completion:nil];
// [self dismissViewControllerAnimated:NO completion:nil];
}
In custom Segue:
-(void)perform
{
UIViewController *srcViewController = (UIViewController *)self.sourceViewController;
UIViewController*destViewController = (UIViewController *)self.destinationViewController;
CATransition *transition = [CATransition animation];
transition.duration = 0.3;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[srcViewController.view.window.layer addAnimation:transition forKey:nil];
[srcViewController presentViewController:destViewController animated:NO completion:nil];
}
......
Somewhere I do it with segue and somewhere with the top method.
What am i doing wrong? i need help please :) Thank you in advance.
If all you need is to trigger the segue programmatically,
why not use -performSegueWithIdentifier:sender: ?

custom segue transition from UIButton

I have to 4 scene on my storyboard and everything works great, but right now I have a modal style with transition cover vertical. I want to have modal style but with a horizontal cover transition.
I change from scene to scene with a round rect button because my scenes does not have a navigation bar (because I don't need it).
I found a tutorial and I create a new class to override the perform method.
Here is my implementation file with the perfom method.
#import "JHCustomSegue.h"
#import "QuartzCore/QuartzCore.h"
#implementation JHCustomSegue
- (void) perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
}
#end
But the transition doesn't work I can chance to any of my scene I always stay in the first screen.
What I'm doing wrong, because the example works very well but they use a navigation bar.
Thanks in advance for your help.

iOs Segue animation left to right (horizontal)

I'm nearly a newbie to Xcode 4.
There's a way to add to a segue a custom transition animation that it is not among the four presented by the Interface Builder in the storyboard management?
Specifically, i want an animation similar to the normal "cover vertical", but horizontal. I want the a view to pass to another sliding from left to right (or right to left) instead that from up to bottom as it happens in the "cover vertical" transition.
I tried with the swipe gesture but no fortune: even that is transitioning from up to bottom and anyway, i don't understand why the defaul transition is from up to bottom when the default transition of all the app usually is right to left or left to right, especially in the case you do swipe...
I tried also a programatically way but no fortune even in this case, using this code:
#import "JHCustomSegue.h"
#implementation JHCustomSegue
- (void) perform {
UIViewController *src = (UIViewController *) self.sourceViewController;
UIViewController *dst = (UIViewController *) self.destinationViewController;
[UIView transitionWithView:src.navigationController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[src presentModalViewController:dst animated:NO];
}
completion:NULL];
}
#end
In the interface builder i defined this class as the class of my segue. Using breakpoint, i saw it enter the function perfom but... don't perform!
I've blocked the app in portrait mode (don't know if it's a problem). I tried to run the application on a real ipad and on a simulated iphone. Same Problem.
You can use CATransition within a custom Segue to achieve left to right transition. Add this #import "QuartzCore/QuartzCore.h" to your custom Segue
-(void)perform {
__block UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
__block UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
}
Based on Zakir Hyder's submission, here is the same functionality translated to Swift:
override func perform() {
var sourceViewController = self.sourceViewController as UIViewController
var destinationViewController = self.destinationViewController as UIViewController
var transition: CATransition = CATransition()
transition.duration = 0.25
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
sourceViewController.navigationController?.view.layer.addAnimation(transition, forKey: "kCATransition")
sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)
}
Here is the working code for custom seque when not using navigation controller.
-(void)perform
{
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromRight; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[destinationController.view.layer addAnimation:transition forKey:kCATransition];
[sourceViewController presentViewController:destinationController animated:NO completion:nil];
}
Check out this Example on Github:
https://github.com/itinance/iOSCustomSegue
It uses a custom segue from right to left in an example app explicitly without UINavigationController as the thread opener where asking for.
The Code in the answer of Sahil didn't work for me at iOS 8. So i had to investigate a little bit more on this issue.
'UIView animateWithDuration' works on iOS 8, while 'destinationController.view.layer addAnimation:transition' does nothing in combination with presentViewController.
I tried using Segue to do the same effect too, but without success. Instead, I used a workaround:
// get the view that's currently showing
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];
UIView *newView = aTwoViewController.view;
// remove the current view and replace with myView1
[currentView removeFromSuperview];
[theWindow addSubview:newView];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToView2"];
You can download the sample project here. Cheers!

Resources