使用动画更改UIView背景颜色

Kex*_*Kex 4 objective-c uiview caanimation uiviewanimation ios

我想UIView用过渡动画改变我的背景颜色.例如,如果视图为红色,我将其更改为蓝色.蓝色将从屏幕底部向上滑动到顶部并填满整个屏幕.我想通过制作UIView具有所需颜色的相同尺寸,然后从屏幕上直到顶部制作动画来实现这一点.尽管如此,这似乎并不是一种非常优雅的方式.有没有更好的方法呢?任何积分都会非常感激.谢谢!

Luc*_*ang 10

试试这个:

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"];
Run Code Online (Sandbox Code Playgroud)

你可以尝试一下:

[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];
Run Code Online (Sandbox Code Playgroud)

我还有另一个建议,使过渡顺利:

[UIView animateWithDuration:2.0 animations:^{
    self.view.layer.backgroundColor = [UIColor blueColor].CGColor;
} completion:nil];
Run Code Online (Sandbox Code Playgroud)