动画 ios 应用中的黑色过渡
black transition in animation ios app
在我的场景转换之间,丑陋的黑色背景短暂出现。有人告诉我,删除动画和延迟可以防止这种情况发生,但这并没有奏效。
它只影响测验问题之间的转换 - 应用中的其他页面都没有问题。这些问题的 json 文件是后端文件。
这里是问题展示的动画代码engine.m
-(void)attachDelegate:(id<QuestionViewControllerDelegate>)delegate{
self.questionController1.delegate = delegate;
self.questionController2.delegate = delegate;
}
-(BOOL)showNextQuestion:(NSArray*)questions inMainView:(UIView*)mainView{
if(self.nextIndex < questions.count){
QuestionViewController* controller = self.nextIndex % 2 == 0 ?
self.questionController1 : self.questionController2;
QuestionViewController* currentController = self.nextIndex % 2 == 1 ?
self.questionController1 : self.questionController2;
controller.question = questions[self.nextIndex];
if(self.nextIndex == 0){
[controller.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[mainView addSubview:controller.view];
ConstraintsPackage* controllerPackage = [self
addConstraintsToSuperview:mainView subview:controller.view];
controller.view.tag = 0;
[currentController.view
setTranslatesAutoresizingMaskIntoConstraints:NO];
[mainView addSubview:currentController.view];
ConstraintsPackage* currentControllerPackage = [self
addConstraintsToSuperview:mainView
subview:currentController.view];
currentController.view.tag = 1;
self.constraintPackages = @[controllerPackage,
currentControllerPackage];
}
[controller loadData];
[self removeController:currentController andShowController:controller
inMainView:mainView];
self.nextIndex++;
return YES;
}else{
[self.questionController1.view removeFromSuperview];
[self.questionController2.view removeFromSuperview];
return NO;
}
}
-(void)removeController:(UIViewController*)fromViewController
andShowController:(UIViewController*)toViewController inMainView:
(UIView*)mainView{
NSTimeInterval duration = 0.3;
ConstraintsPackage* fromPackage = (ConstraintsPackage*)self.constraintPackages[fromViewController.view.tag];
ConstraintsPackage* toPackage = (ConstraintsPackage*)self.constraintPackages[toViewController.view.tag];
CGFloat toValue = 0;
CGFloat fromValue = mainView.frame.size.width;
[mainView layoutIfNeeded];
[UIView animateWithDuration:duration animations:^{
fromPackage.centerXConstraint.constant = -fromValue;
toPackage.centerXConstraint.constant = toValue;
[mainView layoutIfNeeded];
} completion:^(BOOL finished) {
fromPackage.centerXConstraint.constant = 2*mainView.frame.size.width;
}];
if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
[fromViewController viewDidAppear:YES];
[toViewController viewDidAppear:YES];
}
}
这是来自 dropanimationcontroller.m
的代码
@implementation DropAnimationController
-(id)init{
self = [super init];
if(self){
self.presentationDuration = 0.6;
self.dismissalDuration = 0.6;
}
return self;
}
-(NSTimeInterval)transitionDuration:
(id<UIViewControllerContextTransitioning>)transitionContext{
return self.isPresenting ? self.presentationDuration :
self.dismissalDuration;
}
-(void)animateTransition:
(id<UIViewControllerContextTransitioning>)transitionContext{
if(self.isPresenting){
[self executePresentationAnimation:transitionContext];
}
else{
[self executeDismissalAnimation:transitionContext];
}
}
-(void)executePresentationAnimation:
(id<UIViewControllerContextTransitioning>)transitionContext{
UIView* inView = [transitionContext containerView];
UIViewController* toViewController = [transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* blurredView = [self getBlurredImage:fromViewController.view];
blurredView.alpha = 0.0;
[inView addSubview:blurredView];
[inView addSubview:toViewController.view];
CGPoint centerOffScreen = inView.center;
centerOffScreen.y = (-1)*inView.frame.size.height;
toViewController.view.center = centerOffScreen;
[UIView animateWithDuration:self.presentationDuration delay:0.0f
usingSpringWithDamping:0.4f initialSpringVelocity:6.0f
options:UIViewAnimationOptionCurveEaseIn animations:^{
toViewController.view.center = inView.center;
fromViewController.view.alpha = 0.6;
blurredView.alpha = 1.0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
-(void)executeDismissalAnimation:
(id<UIViewControllerContextTransitioning>)transitionContext{
UIView* inView = [transitionContext containerView];
UIViewController* toViewController = [transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* blurredView = [self getBlurredImage:toViewController.view];
blurredView.alpha = 1.0;
[inView addSubview:blurredView];
[inView insertSubview:toViewController.view
belowSubview:fromViewController.view];
CGPoint centerOffScreen = inView.center;
centerOffScreen.y = (-1)*inView.frame.size.height;
[UIView animateKeyframesWithDuration:self.dismissalDuration delay:0.0f
options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5
animations:^{
CGPoint center = fromViewController.view.center;
center.y += 50;
fromViewController.view.center = center;
blurredView.alpha = 0.0;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5
animations:^{
fromViewController.view.center = centerOffScreen;
toViewController.view.alpha = 1.0;
}];
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
-(UIView*)getBlurredImage:(UIView*)view{
UIGraphicsBeginImageContext(view.frame.size);
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage* blurredImage = [image applyBlurWithRadius:2.0 tintColor:[UIColor
colorWithWhite:0.0 alpha:0.4f] saturationDeltaFactor:1 maskImage:nil];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.image = blurredImage;
return imageView;
}
@end
我认为它采用 window 的背景颜色。
请试试这个,它可能对你有帮助。
self.window.backgroundColor=[UIColor whiteColor];
在我的场景转换之间,丑陋的黑色背景短暂出现。有人告诉我,删除动画和延迟可以防止这种情况发生,但这并没有奏效。
它只影响测验问题之间的转换 - 应用中的其他页面都没有问题。这些问题的 json 文件是后端文件。
这里是问题展示的动画代码engine.m
-(void)attachDelegate:(id<QuestionViewControllerDelegate>)delegate{
self.questionController1.delegate = delegate;
self.questionController2.delegate = delegate;
}
-(BOOL)showNextQuestion:(NSArray*)questions inMainView:(UIView*)mainView{
if(self.nextIndex < questions.count){
QuestionViewController* controller = self.nextIndex % 2 == 0 ?
self.questionController1 : self.questionController2;
QuestionViewController* currentController = self.nextIndex % 2 == 1 ?
self.questionController1 : self.questionController2;
controller.question = questions[self.nextIndex];
if(self.nextIndex == 0){
[controller.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[mainView addSubview:controller.view];
ConstraintsPackage* controllerPackage = [self
addConstraintsToSuperview:mainView subview:controller.view];
controller.view.tag = 0;
[currentController.view
setTranslatesAutoresizingMaskIntoConstraints:NO];
[mainView addSubview:currentController.view];
ConstraintsPackage* currentControllerPackage = [self
addConstraintsToSuperview:mainView
subview:currentController.view];
currentController.view.tag = 1;
self.constraintPackages = @[controllerPackage,
currentControllerPackage];
}
[controller loadData];
[self removeController:currentController andShowController:controller
inMainView:mainView];
self.nextIndex++;
return YES;
}else{
[self.questionController1.view removeFromSuperview];
[self.questionController2.view removeFromSuperview];
return NO;
}
}
-(void)removeController:(UIViewController*)fromViewController
andShowController:(UIViewController*)toViewController inMainView:
(UIView*)mainView{
NSTimeInterval duration = 0.3;
ConstraintsPackage* fromPackage = (ConstraintsPackage*)self.constraintPackages[fromViewController.view.tag];
ConstraintsPackage* toPackage = (ConstraintsPackage*)self.constraintPackages[toViewController.view.tag];
CGFloat toValue = 0;
CGFloat fromValue = mainView.frame.size.width;
[mainView layoutIfNeeded];
[UIView animateWithDuration:duration animations:^{
fromPackage.centerXConstraint.constant = -fromValue;
toPackage.centerXConstraint.constant = toValue;
[mainView layoutIfNeeded];
} completion:^(BOOL finished) {
fromPackage.centerXConstraint.constant = 2*mainView.frame.size.width;
}];
if (SYSTEM_VERSION_LESS_THAN(@"8.0")) {
[fromViewController viewDidAppear:YES];
[toViewController viewDidAppear:YES];
}
}
这是来自 dropanimationcontroller.m
的代码@implementation DropAnimationController
-(id)init{
self = [super init];
if(self){
self.presentationDuration = 0.6;
self.dismissalDuration = 0.6;
}
return self;
}
-(NSTimeInterval)transitionDuration:
(id<UIViewControllerContextTransitioning>)transitionContext{
return self.isPresenting ? self.presentationDuration :
self.dismissalDuration;
}
-(void)animateTransition:
(id<UIViewControllerContextTransitioning>)transitionContext{
if(self.isPresenting){
[self executePresentationAnimation:transitionContext];
}
else{
[self executeDismissalAnimation:transitionContext];
}
}
-(void)executePresentationAnimation:
(id<UIViewControllerContextTransitioning>)transitionContext{
UIView* inView = [transitionContext containerView];
UIViewController* toViewController = [transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* blurredView = [self getBlurredImage:fromViewController.view];
blurredView.alpha = 0.0;
[inView addSubview:blurredView];
[inView addSubview:toViewController.view];
CGPoint centerOffScreen = inView.center;
centerOffScreen.y = (-1)*inView.frame.size.height;
toViewController.view.center = centerOffScreen;
[UIView animateWithDuration:self.presentationDuration delay:0.0f
usingSpringWithDamping:0.4f initialSpringVelocity:6.0f
options:UIViewAnimationOptionCurveEaseIn animations:^{
toViewController.view.center = inView.center;
fromViewController.view.alpha = 0.6;
blurredView.alpha = 1.0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
-(void)executeDismissalAnimation:
(id<UIViewControllerContextTransitioning>)transitionContext{
UIView* inView = [transitionContext containerView];
UIViewController* toViewController = [transitionContext
viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController* fromViewController = [transitionContext
viewControllerForKey:UITransitionContextFromViewControllerKey];
UIView* blurredView = [self getBlurredImage:toViewController.view];
blurredView.alpha = 1.0;
[inView addSubview:blurredView];
[inView insertSubview:toViewController.view
belowSubview:fromViewController.view];
CGPoint centerOffScreen = inView.center;
centerOffScreen.y = (-1)*inView.frame.size.height;
[UIView animateKeyframesWithDuration:self.dismissalDuration delay:0.0f
options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5
animations:^{
CGPoint center = fromViewController.view.center;
center.y += 50;
fromViewController.view.center = center;
blurredView.alpha = 0.0;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5
animations:^{
fromViewController.view.center = centerOffScreen;
toViewController.view.alpha = 1.0;
}];
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
-(UIView*)getBlurredImage:(UIView*)view{
UIGraphicsBeginImageContext(view.frame.size);
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage* blurredImage = [image applyBlurWithRadius:2.0 tintColor:[UIColor
colorWithWhite:0.0 alpha:0.4f] saturationDeltaFactor:1 maskImage:nil];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.image = blurredImage;
return imageView;
}
@end
我认为它采用 window 的背景颜色。 请试试这个,它可能对你有帮助。
self.window.backgroundColor=[UIColor whiteColor];