2

I'm learning how to implement custom view transition. I want to create a slide menu something like spotify side menu. I'm using UIViewControllerAnimatedTransitioning. The animation seems to be working but after dismissing transition, the screen become blank.

This is the code for presenting transition

#import "Transition.h"

@implementation Transition

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{

UIViewController *to=[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *from=[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

UIView *containerView=[transitionContext containerView];
to.view.alpha=0.0;


[containerView addSubview:to.view];
[containerView addSubview:from.view];

[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
    from.view.frame = CGRectMake(200, 0, from.view.frame.size.width, from.view.frame.size.height);
    from.view.alpha=1.0;
    to.view.alpha=1.0;
} completion:^(BOOL finished) {
    [transitionContext completeTransition:YES];
}];

}
@end

This is the code for dismissal transition

#import "DismissTransition.h"

@implementation DismissTransition

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
return 1;
}

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContex{

UIViewController *from=[transitionContex viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *to=[transitionContex viewControllerForKey:UITransitionContextToViewControllerKey];

UIView *containerView=[transitionContex containerView];


[UIView animateWithDuration:[self transitionDuration:transitionContex] animations:^{
    to.view.frame = CGRectMake(0, 0, from.view.frame.size.width, from.view.frame.size.height);
    to.view.alpha=1.0;
    from.view.alpha=0.0;
} completion:^(BOOL finished) {
    [transitionContex completeTransition:YES];
}];

}
@end

This is where my custom transition is called

#import "ViewController.h"
#import "Transition.h"
#import "DismissTransition.h"
#import "MenuViewController.h"

@interface ViewController ()<UIViewControllerTransitioningDelegate>

@end

@implementation ViewController
- (IBAction)buttonTap:(id)sender {
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MenuViewController* viewController = [mystoryboard  instantiateViewControllerWithIdentifier:@"MenuVC"];


viewController.modalPresentationStyle=UIModalPresentationCustom;
viewController.transitioningDelegate=self;

[self presentViewController:viewController animated:YES completion:nil];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                              presentingController:(UIViewController *)presenting
                                                                  sourceController:(UIViewController *)source{

return [[Transition alloc]init];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{

return [[DismissTransition alloc]init];
}



@end

And this is how I dismiss MenuViewController

#import "MenuViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController
- (IBAction)doneButtonTap:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



@end
cloudy45man
  • 391
  • 2
  • 19
  • I'm supposing you're developing on the iOS 8 SDK, so check the discussion on http://stackoverflow.com/questions/25588617/ios-8-screen-blank-after-dismissing-view-controller-with-custom-presentation and the answer by DJSK – spassas Oct 21 '14 at 12:05
  • Yes, I've already seen your answer. But my problem is that i need to present both presenting view and presented view at the same time. So, it can make an effect, a slide menu view appear from left of original view. So I need to add fromview into the container, otherwise, fromview will move to right, fade in and disappear. I doubt there might be problem in the menu view dismissing. And i am also not very sure if this is the right approach to create this effect. – cloudy45man Oct 21 '14 at 14:48

1 Answers1

0

You should add to.view to containerView in DismissTransition

Doon
  • 97
  • 3