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

Posted

技术标签:

【中文标题】自定义视图转换后屏幕变为空白【英文标题】:Screen become blank after custom view transition 【发布时间】:2014-10-21 10:04:41 【问题描述】:

我正在学习如何实现自定义视图转换。我想创建一个类似于 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

【问题讨论】:

我假设您正在使用 ios 8 SDK 进行开发,因此请查看***.com/questions/25588617/… 上的讨论以及 DJSK 的回答 是的,我已经看到你的回答了。但我的问题是我需要同时呈现呈现视图和呈现视图。因此,它可以产生效果,从原始视图的左侧出现一个滑动菜单视图。所以我需要将fromview添加到容器中,否则,fromview会向右移动,淡入并消失。我怀疑菜单视图关闭可能存在问题。而且我也不太确定这是否是创造这种效果的正确方法。 【参考方案1】:

你应该在 DismissTransition 中将 to.view 添加到 containerView

【讨论】:

以上是关于自定义视图转换后屏幕变为空白的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 创建自定义过渡动画

自定义转换后,搜索栏从视图层次结构中删除

视图是黑色的,当返回到导航控制器堆栈视图时,在实现自定义转换后

在自定义模式解除转换后,第一个视图控制器的生命周期方法不会被调用

SwiftUI 如何获取自定义幻灯片转换的视图大小?

为什么ListView与自定义适配器显示什么?