简体   繁体   English

我可以为CATransition设置最大alpha吗?

[英]Can I set maximum alpha for CATransition?

Is it possible to adjust the maximum alpha (opacity) in a CATransition for when the views fade in and out? 当视图淡入和淡出时,是否可以在CATransition调整最大Alpha(不透明度)?

What I want is something that looks like a modal segue. 我想要的是看起来像是模态的segue。 Compared to a default modal segue, the transition given by CATransition is very «dramatic». 与默认模态CATransition相比, CATransition给出的过渡非常“戏剧性”。

Say I have two views. 说我有两种看法。 A: the view I want to transition from. 答:我要从中转换的视图。 B: the view I want to transition to. B:我想过渡到的视图。

I want B to come moving in from the bottom over A. For this I use the kCATransitionMoveIn type, with subtype kCATransitionFromTop (which is weird because B is moving up from bottom, not top). 我希望B从A的底部移入。为此,我使用了kCATransitionMoveIn类型,其子类型为kCATransitionFromTop (这很奇怪,因为B从底部而不是顶部向上移动)。

In the default modal segue, this works fine, and A is only greyed out a little. 在默认模态segue中,这可以正常工作,并且A只会变灰一点。 With CATransition , A is totally black at towards the end of the transition when B has moved about 70% over A. 使用CATransition ,当B移到A上方约70%时,A在过渡结束时全黑。

Code: 码:

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;

The problem originates from here . 问题出在这里

After digging around some more I figured that you simply can't set a maximum opacity / alpha for the CATransition that you get out of the box. 在深入研究之后,我发现您无法为开箱即用的CATransition设置最大不透明度/ alpha。

So I solved it like this: 所以我这样解决了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM