開発
Custom Push Segue
will
The Storyboard of Xcode provides fast and connivent tool to develop the views and transition.
Developer can use Segue to define the transition from one view to another. However, if we just use the predefined segue (push and modal), the direction of transition is fixed. For instance, the PUSH will be from right to left.
Some times, we need different directions with the predefined ones. In this case, we can custom the segue.
Give the example about customing push segue to make the transition from left to right:
1. Create class “LeftToRightPushSegue”, which externs from UIStoryboardSegue
#import "LeftToRighPushSegue.h" #import <QuartzCore/QuartzCore.h> @implementation LeftToRighPushSegue -(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]; } @end
2. When the view is ready to pop up from stack, the animation of transition also need to be changed, otherwise, the view will be poped up with default direction:
-(void)pushBack:(id)sender { //Setting the direction of popup from right to left CATransition *transition = [CATransition animation]; transition.duration = .25; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.type = kCATransitionPush; transition.subtype = kCATransitionFromRight; [self.navigationController.view.layer addAnimation:transition forKey:kCATransition]; [self.navigationController popViewControllerAnimated:YES]; }