首页 > 解决方案 > ios动画中的黑色背景

问题描述

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

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];

标签: iosobjective-cxcode

解决方案


我建议您使用 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];
}];

推荐阅读