首页 > 解决方案 > 我有一个带有自定义 UIStoryboardSegue 对象的小型测试项目,但动画有黑色阴影。有什么办法可以去掉?

问题描述

我正在 Xcode 10 for iOS 中开发一个小型测试项目。我基本上是在测试自定义 UIStoryboardSegues。我准备了一个测试项目来证明这种不当行为。

在转场动画中,屏幕上会出现黑色阴影。你可以在测试项目中看到它,因为我已经将动画时间设置为 5 秒。任何帮助是极大的赞赏。谢谢

标签: objective-ccocoa-touchuistoryboardsegue

解决方案


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

IE:

// 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;
    }
});

您还需要将动画切换到 sourceView 的图层:

[sourceViewController.view.layer addAnimation:transition forKey:nil];

但这实际上只是一种使它看起来更好的解决方法。我强烈建议不要使用自定义 Storyboard Segues,而是使用自定义动画器,它可以让您更好地控制动画过渡。

我的答案是:IOS/Objective-C:在没有 Storyboard Segue 的情况下,可以在模态转换中使用自定义 Segue?有自定义 SlideUp/SlideDown 动画师的完整示例。

这里是它的文档链接:

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/CustomizingtheTransitionAnimations.html

https://developer.apple.com/documentation/uikit/uiviewcontrollertransitioningdelegate?language=objc

https://developer.apple.com/documentation/uikit/uiviewcontrolleranimatedtransitioning?language=objc


推荐阅读