IOS/Objective-C/Storyboard:从左到右的自定义 Segue 在视图控制器之间创建黑条
IOS/Objective-C/Storyboard: Custom Segue from Left to Right Creating Black Bar Between View Controllers
我已将 UIStoryboardSegue 子类化以创建从左到右的 Segue。 segue 有效,但是,在两个 View Controller 之间暂时可见的黑色垂直条或背景会破坏效果。 (我希望 segue 看起来像正常的从右到左显示 segue,但方向相反)。有谁知道可能导致这种情况的原因或如何摆脱它?这是我的代码:
#import "customSegue.h"
#import "QuartzCore/QuartzCore.h"
@implementation customSegue
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
}
@end
编辑:
虽然 none 对我有用,但我想我会收集一些我发现的建议修复。
将呈现视图控制器的背景颜色设置为白色
Set self.definesPresentationContext = YES;
在呈现视图控制器上或检查 "Defines Context" 以获取已接受答案 .
中故事板中的 VC
指定上下文如下:
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
UIViewController *destinationController = (UIViewController*)[self destinationViewController]
[sourceViewController presentViewController:destinationController animated:NO completion:nil];
来自已接受的答案 here。
您看到的是视图在动画开始和结束附近不断变化的不透明度,因此您看到的 "black bar" 实际上是背景 window。虽然它可能并不完美,但一个快速的解决方法是更改 window 的背景颜色以匹配目标视图控制器的背景颜色(如果需要,然后在转换完成后将其设置回来)。
这是经过必要修改的代码示例:
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
CGFloat animationDuration = 0.25f;
transition.duration = animationDuration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
transition.fillMode = kCAFillModeForwards;
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
// hold onto the previous window background color
UIColor *previousWindowBackgroundColor = sourceViewController.view.window.backgroundColor;
// switch the window background color to match the destinationController's background color temporarily
sourceViewController.view.window.backgroundColor = destinationController.view.backgroundColor;
// do the transition
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
// switch the window color back after the transition duration from above
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// make sure we still have a handle on the destination controller
if (destinationController) {
destinationController.view.window.backgroundColor = previousWindowBackgroundColor;
}
});
}
这里有一个过渡减慢到 2 秒的例子:
Slowed down transition
还有代码中 0.25 处的动画:
Your speed transition
我已将 UIStoryboardSegue 子类化以创建从左到右的 Segue。 segue 有效,但是,在两个 View Controller 之间暂时可见的黑色垂直条或背景会破坏效果。 (我希望 segue 看起来像正常的从右到左显示 segue,但方向相反)。有谁知道可能导致这种情况的原因或如何摆脱它?这是我的代码:
#import "customSegue.h"
#import "QuartzCore/QuartzCore.h"
@implementation customSegue
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
transition.duration = .25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
}
@end
编辑:
虽然 none 对我有用,但我想我会收集一些我发现的建议修复。
将呈现视图控制器的背景颜色设置为白色
Set self.definesPresentationContext = YES;
在呈现视图控制器上或检查 "Defines Context" 以获取已接受答案
指定上下文如下:
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
UIViewController *destinationController = (UIViewController*)[self destinationViewController]
[sourceViewController presentViewController:destinationController animated:NO completion:nil];
来自已接受的答案 here。
您看到的是视图在动画开始和结束附近不断变化的不透明度,因此您看到的 "black bar" 实际上是背景 window。虽然它可能并不完美,但一个快速的解决方法是更改 window 的背景颜色以匹配目标视图控制器的背景颜色(如果需要,然后在转换完成后将其设置回来)。
这是经过必要修改的代码示例:
-(void)perform {
UIViewController *sourceViewController = (UIViewController*)[self sourceViewController];
UIViewController *destinationController = (UIViewController*)[self destinationViewController];
CATransition* transition = [CATransition animation];
CGFloat animationDuration = 0.25f;
transition.duration = animationDuration;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
transition.fillMode = kCAFillModeForwards;
[sourceViewController.navigationController.view.layer addAnimation:transition
forKey:kCATransition];
// hold onto the previous window background color
UIColor *previousWindowBackgroundColor = sourceViewController.view.window.backgroundColor;
// switch the window background color to match the destinationController's background color temporarily
sourceViewController.view.window.backgroundColor = destinationController.view.backgroundColor;
// do the transition
[sourceViewController.navigationController pushViewController:destinationController animated:NO];
// switch the window color back after the transition duration from above
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// make sure we still have a handle on the destination controller
if (destinationController) {
destinationController.view.window.backgroundColor = previousWindowBackgroundColor;
}
});
}
这里有一个过渡减慢到 2 秒的例子:
Slowed down transition
还有代码中 0.25 处的动画:
Your speed transition