【问题标题】:Black background in ios animationios动画中的黑色背景
【发布时间】: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];

【问题讨论】:

    标签: ios objective-c xcode


    【解决方案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];
    }];
    

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 2013-12-03
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 2015-06-09
      • 2017-11-10
      相关资源
      最近更新 更多