使用动画更改 UIView 背景颜色 [英] Changing UIView background color using an animation

查看:35
本文介绍了使用动画更改 UIView 背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用过渡动画更改我的 UIView 背景颜色.例如,如果视图为红色,而我将其更改为蓝色.蓝色会从屏幕底部向上滑动到顶部并填满整个屏幕.我正在考虑通过制作一个具有所需颜色的相同大小的 UIView 来实现这一点,然后将其从屏幕外动画到顶部.不过,这似乎不是一种非常优雅的方式.有没有更好的方法来做到这一点?任何要点将不胜感激.谢谢!

I want to change my UIView background color with a transition animation. For example, if the view is red and I change it to blue. The blue color will slide up from the bottom of the screen to the top and fill the whole screen. I was thinking of doing this by making a UIView of identical size with the desired color and then animating it from off screen up to the top. This doesn't seem like a very elegant way to do it though. Is there any better way of doing this? Any points would be really appreciated. Thanks!

推荐答案

试试这个:

CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
layer.backgroundColor = [UIColor blueColor].CGColor;
[self.view.layer addSublayer:layer];

CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position.y";
animation.byValue = @(-self.view.bounds.size.height);
animation.duration = 1;

animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;

[layer addAnimation:animation forKey:@"Splash"];

你可以试试这个:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp
                       forView:self.view cache:NO];
self.view.layer.backgroundColor = [UIColor blueColor].CGColor;
[UIView commitAnimations];

我还有一个建议可以让过渡顺利进行:

I also have another suggestion that make the transition smooth:

[UIView animateWithDuration:2.0 animations:^{
    self.view.layer.backgroundColor = [UIColor blueColor].CGColor;
} completion:nil];

这篇关于使用动画更改 UIView 背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆