Custom Segue and Custom Unwind Segue with Animations using Navigation Controller - Part 2/2
IOS 8.1 Xcode 6.1 tutorial: Customize Segue and UnwindSegue
Our goal this animation between the 2 ViewController:
6 Step need:
1. Create Custom Segue class and “-(void)perform” code
2. Change the Segue class on the storyboard
3. Test your Custom Segue in IOS Simulator
4. Create UnwindSegue
5. Detect MyCustomUnwindSegue on Storyboard
6. Test your CustomUnwindSegue and add color to your window background
Check the video, which just 6 minute! :)
If you are beginner, check the 1 part!
1. Create Custom Segue class and “-(void)perform” code
1.1. Right click New File — Cocoa Touch Class - name: MyCustomSegue, type: UIStoryboardSegue
1.2. in MyCustomSegue.m file create “-(void)perform” code:
You can refer to source VieController and destination ViewController, simply, and you can use the MyCustomSegue several places on the storyboard, regardless of which specific ViewController use it.
1.3. Put the animation into the -(void)perform code:
For example just come the destination ViewController from up to down, and source ViewController just goes down. So the full perform code looks like this:
2. Change the Segue class on the storyboard
2.1. Click on the segue, and in the Attributes inspector change the Segue from “Show” to “Custom”.
4.1. Repeat the first Step, so add New File with MyCustomUnwindSegue name…
4.2. …and copy perform code but change the y coordinates in the UIViewAnimation to move up your destination ViewController and source too.
Attention! - Changes:
4.3. the easiest way is to copy the code from here. :)
4.4.In ViewController.m file create the IBAction UIStoryboardSegue to handle the custom unwind segue, and add if statement to change the background to orangeColor:
Now you have 2 type of UnwindSegue in your code. The first is the normal UnwindSegue, and the second your own CustomUnwindSegue. So if you use CustomUnwindSegue, and would like to do some changes after segue animation, you can code here in returnedFromSegue
Your code looks like this:
4.5. Add returnedFromSegue to your new UnwindSegue at your SecondViewController’s button.
- ctrl drag to Exit, and choose returnedFromSegue
- click on the UnwindSegue, and add UnwindFromSecondView as Identifier.
5. Detect MyCustomUnwindSegue on Storyboard
5.1. Do in ViewController.m : import MyCustomUnwindSegue.h file
5.2. If you not use NavigationController just put this segueForUnwindingToViewController:code in ViewController.m, this will be detect which segue should be use MyCustomUnwindSegue class:
5.3. If you use NavigationController on your first ViewController (where comes the UnwindSegue), you should subclass your navcontroller:
5.3.1. Add New Cocoa Touch Class File, name: MyNavController, type: UINavigationController
5.3.2. On Storyboard add your new class to your NavigationController.
5.3.2. In MyNavController.m file import MyCustomUnwindSegue.h file, and put the segueForUnwindingToViewController:code. So your MyNavController file looks like this:
6. Test your CustomUnwindSegue and add color to your window background
6.1. Play in IOS Simulator.
If you would like to some other color instead of black window background, just put this color code in MyCusotmUnwindSegue.m file
window.backgroundColor = [UIColor redColor];
looks like this:
6.2. And test again with red window background color:
Download code
You can download the whole tutorial from github:MyCustomSegue.git
If you would like to be notified of my new solutions, please subscribe to my youtube channel and/ or follow me on twitter!