IOS / Objective-C / Storyboard:从左到右的自定义Segue在视图控制器之间创建黑条

时间:2018-09-07 20:46:24

标签: 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

编辑:

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

将演示视图控制器的背景色设置为白色

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中获得。

1 个答案:

答案 0 :(得分: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