gpt4 book ai didi

ios - 使用动画更改 UIView 背景颜色

转载 作者:可可西里 更新时间:2023-11-01 05:03:09 25 4
gpt4 key购买 nike

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

最佳答案

试试这个:

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];

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

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

关于ios - 使用动画更改 UIView 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30380910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com