ios动画中的黑色背景

Posted

技术标签:

【中文标题】ios动画中的黑色背景【英文标题】:Black background in ios animation 【发布时间】:2021-02-18 21:48:18 【问题描述】:

使用动画更改控制器时,我看到背景为黑色。我试图以不同的方式改变它,但没有任何效果。怎么办?

self.view.backgroundColor = [UIColor whiteColor];
self.view.layer.backgroundColor = [UIColor whiteColor].CGColor;
self.view.window.backgroundColor = [UIColor whiteColor];
self.view.window.layer.backgroundColor = [UIColor whiteColor].CGColor;
UIApplication.sharedApplication.windows.firstObject.backgroundColor = [UIColor whiteColor];
[UIApplication sharedApplication].windows.lastObject.rootViewController.view.backgroundColor = [UIColor whiteColor];
[UIApplication sharedApplication].windows.lastObject.rootViewController.view.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIApplication sharedApplication].windows.firstObject.rootViewController.view.backgroundColor = [UIColor whiteColor];
[UIApplication sharedApplication].windows.firstObject.rootViewController.view.layer.backgroundColor = [UIColor whiteColor].CGColor;
[UIApplication sharedApplication].windows.firstObject.screen.focusedView.backgroundColor = [UIColor whiteColor];
UIScene *scene = [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];
if([scene.delegate conformsToProtocol:@protocol(UIWindowSceneDelegate)])
    UIWindow *window = [(id <UIWindowSceneDelegate>)scene.delegate window];
    window.backgroundColor = [UIColor whiteColor];


ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
CATransition* transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromLeft;
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:vc animated:NO completion:nil];

【问题讨论】:

【参考方案1】:

我建议您使用 UIViewControllerTransitioningDelegate 和 UIViewControllerAnimatedTransitioning 重构您的代码。

您还可以使用 CGAffineTransform 创建 UIView 动画来转换视图,如下所示:

 ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self.view.superview addSubview:vc.view];
CGAffineTransform firstTransform = CGAffineTransformMakeTranslation(-1 * self.view.frame.size.width, 0);
vc.view.transform = firstTransform;

[UIView animateWithDuration:0.50 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^
    vc.view.transform =  CGAffineTransformTranslate(firstTransform, self.view.frame.size.width, 0);
 completion:^(BOOL finished) 
    [self presentViewController:vc animated:NO completion:nil];
];

【讨论】:

以上是关于ios动画中的黑色背景的主要内容,如果未能解决你的问题,请参考以下文章

imagemagick - 具有静态黑色背景的动画?

关于Activity切换动画(overridePendingTransition)的黑色背景问题

动画PNG图像不流畅,在IE中得到黑色背景

iOS 9:按钮上的黑色背景,以及某些设备上的性能错误

iOS 7 UINavigationController 过渡到 UITableView:动画期间的黑色半透明层

在ios 7中将UI状态栏的背景颜色设置为黑色