【问题标题】:Can I set maximum alpha for CATransition?我可以为 CATransition 设置最大 alpha 吗?
【发布时间】:2014-03-24 09:37:04
【问题描述】:

是否可以在 CATransition 中调整视图淡入淡出的最大 alpha(不透明度)?

我想要的是看起来像模态转场的东西。与默认的模态转场相比,CATransition 给出的过渡非常“戏剧化”。

假设我有两种观点。 A:我想从中过渡的观点。 B:我要转换到的视图。

我希望 B 从底部进入 A。为此,我使用 kCATransitionMoveIn 类型和子类型 kCATransitionFromTop(这很奇怪,因为 B 是从底部向上移动,而不是从顶部向上移动)。

在默认的模态序列中,这可以正常工作,并且 A 只是变灰了一点。使用CATransition,A 在过渡结束时完全是黑色的,此时 B 已经比 A 移动了大约 70%。

代码:

UIStoryboard *loginBoard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
UIViewController *vc = [loginBoard instantiateInitialViewController];

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];

[keyWindow insertSubview:vc.view belowSubview:keyWindow.rootViewController.view];

CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;

[keyWindow.layer addAnimation:transition forKey:kCATransition];

[keyWindow.rootViewController.view removeFromSuperview];
keyWindow.rootViewController = vc;

问题源于here

【问题讨论】:

    标签: ios iphone objective-c cocoa-touch


    【解决方案1】:

    在深入挖掘之后,我发现您根本无法为开箱即用的 CATransition 设置最大不透明度/alpha。

    所以我这样解决了:

    //offset the frame
    vc.view.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
    [UIView animateWithDuration:5
         animations:^{
             //insert over/before root view instead of after
             [keyWindow insertSubview:vc.view aboveSubview:keyWindow.rootViewController.view];
             vc.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
             keyWindow.rootViewController.view.alpha = 0.8;
         }
         completion:^(BOOL finished) {
             NSLog(@"completed! now you can change the rootviewcontroller etc..");
         }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多