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

问题描述 投票:0回答:1

我已经将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个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.