【问题标题】:IOS/Objective-C/Storyboard: Custom Segue from Left to Right Creating Black Bar Between View ControllersIOS/Objective-C/Storyboard:自定义Segue从左到右在视图控制器之间创建黑条
【发布时间】:2019-02-13 05:09:32
【问题描述】:

我将 UIStoryboardSegue 子类化以创建从左到右的 Segue。 segue 工作,但是,有一个黑色的垂直条或背景在两个视图控制器之间暂时可见,这会破坏效果。 (我希望 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

编辑:

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

将呈现视图控制器的背景颜色设置为白色

Set self.definesPresentationContext = YES; 在呈现视图控制器上或从接受的答案 here 中检查情节提要中 VC 的“定义上下文”。

指定上下文如下:

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

来自接受的答案here

【问题讨论】:

    标签: ios objective-c storyboard uistoryboardsegue


    【解决方案1】:

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

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

    -(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

    【讨论】:

    • 我同意 Apple 应该有一种更简洁的方法来完成这项工作,但您的方法确实有效!顺便说一句,你估计常规节目 segue 的速度是多少?我尝试了各种值,0.45 似乎是正确的,但很难说。 (也可能取决于过渡风格。在上面的代码中,我使用 kCAMediaTimingFunctionEaseInEaseOut 但苹果说......大多数系统过渡都使用默认值
    • 从推送发生之前到 viewDidAppear: 在第二个 VC 中调用一次的时间计算时,我得到了大约 0.52 秒。您可以使用自己的自定义转换进行类似的时间计算,以尝试使其与之匹配。
    猜你喜欢
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2019-01-11
    相关资源
    最近更新 更多