IOS/objective-C/ Storyboard : Custom Segue from Left to Right Creating Black Bar Between View Controllers

标签 ios objective-c storyboard uistoryboardsegue

我将 UIStoryboardSegue 子类化以创建从左到右的 Segue。 segue 有效,但是,在两个 View Controller 之间暂时可见的黑色垂直条或背景会破坏效果。 (我希望 segue 看起来像正常的从右到左显示 segue,但方向相反)。有谁知道可能导致这种情况的原因或如何摆脱它?这是我的代码:

#import "customSegue.h"
#import "QuartzCore/QuartzCore.h"

@implementation customSegue

-(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; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom



    [sourceViewController.navigationController.view.layer addAnimation:transition
                                                                forKey:kCATransition];

    [sourceViewController.navigationController pushViewController:destinationController animated:NO];

}
@end

编辑:

我想我会收集一些我发现的建议修复,尽管没有一个对我有用。

将呈现 View Controller 的背景颜色设置为白色

在呈现 View Controller 上设置 self.definesPresentationContext = YES; 或从已接受的答案 here 中检查 Storyboard 中 VC 的“定义上下文” .

指定上下文如下:

UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
    UIViewController *destinationController = (UIViewController*)[self destinationViewController]
    [sourceViewController presentViewController:destinationController animated:NO completion:nil];

来自接受的答案 here .

最佳答案

您看到的是 View 在动画开始和结束附近改变不透明度,因此您看到的“黑条”实际上是后备窗口。虽然它可能并不完美,但快速解决方法是更改​​窗口的背景颜色以匹配目标 View Controller 的背景颜色(如果需要,然后在转换完成后将其设置回来)。

这是您的代码示例,并进行了必要的修改:

-(void)perform {
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
    UIViewController *destinationController = (UIViewController*)[self destinationViewController];

    CATransition* transition = [CATransition animation];
    CGFloat animationDuration = 0.25f;
    transition.duration = animationDuration;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
    transition.fillMode = kCAFillModeForwards;



    [sourceViewController.navigationController.view.layer addAnimation:transition
                                                                forKey:kCATransition];
    // hold onto the previous window background color
    UIColor *previousWindowBackgroundColor = sourceViewController.view.window.backgroundColor;
    // switch the window background color to match the destinationController's background color temporarily
    sourceViewController.view.window.backgroundColor = destinationController.view.backgroundColor;
    // do the transition
    [sourceViewController.navigationController pushViewController:destinationController animated:NO];
    // switch the window color back after the transition duration from above
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // make sure we still have a handle on the destination controller
        if (destinationController) {
            destinationController.view.window.backgroundColor = previousWindowBackgroundColor;
        }
    });
}

以下是过渡减慢至 2 秒的示例:

Slowed down transition

代码中的动画为 0.25:

Your speed transition

关于IOS/objective-C/ Storyboard : Custom Segue from Left to Right Creating Black Bar Between View Controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52229444/

相关文章:

objective-c - dispatch_barrier_async 中的代码未执行

objective-c - Swift 编译器与 Objective-C 编译器

xcode - 无法打开此文档 "XXXXX.storyboard"。请使用较新版本的 Xcode

ios - 容器边界内的 UIContainer viewcontroller segue

ios - 如何将 xib 转换为 Storyboard?

ios - 在容器 View 中嵌入多个 View Controller

ios - DateFormatter "HH"没有以 24 小时格式快速给出时间

ios - iOS 有 "touch slop"常量吗?

ios - iPhone SDK - 可能有多个启动画面?

objective-c - NSArray 的实现