自定义视图转换后,屏幕变为空白

时间:2014-10-21 10:04:41

标签: ios

我正在学习如何实现自定义视图转换。我想创建一个像Spotify侧面菜单一样的幻灯片菜单。我正在使用UIViewControllerAnimatedTransitioning。动画似乎正在工作但在解除转换后,屏幕变为空白。

这是用于呈现转换的代码

#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

这是解雇过渡的代码

#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

这是我的自定义转换名称

#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

这就是我解雇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

1 个答案:

答案 0 :(得分:0)

您应该在DismissTransition

中将to.view添加到containerView