gpt4 book ai didi

ios - UIView按预期用animateWithDuration对setBackgroundColor进行动画处理。但是,UILabel立即发生。为什么?

转载 作者:行者123 更新时间:2023-12-01 16:35:23 24 4
gpt4 key购买 nike

我要实现的目标是:拥有多个UILabel并能够依次突出显示它们,这意味着我想更改它们的textColor,fontSize和位置。因此,我已经成功编写了一个演示,该演示接受一组UIView并依次突出显示它们(更改了backgroundColor)。

阅读了Andrew Fuchs对引用Best Way to Perform Several Sequential UIView Animations?Multiple UIView Animations Without Nested Blocks问题的答复

但是,当我在代码中用UILabel代替UIView时,所有内容都掉了下来,而且它们都不是动画,因此更改立即发生。谁能告诉我我在做什么错? UILabel是UIView的子类,因此我希望UIView中可以使用的属性可以在UILabel中使用。在Apple的“iOS的View编程指南-动画”中,指出backgroundColor可以被动画化。因此,我回到基础知识,编写了一些测试代码,发现对于UIView,我可以愉快地为其backgroundColor,alpha和中心位置设置动画。 UILabel alpha属性和中心位置可以成功设置动画,而backgroundColor和textColor无法设置动画。另请:GitHub AnimateSequentially

- (void)viewDidLoad {
[super viewDidLoad];

duration = 2.0;
yOffset = 100.0;
normalColor = [UIColor lightGrayColor];
highlightedColor = [UIColor greenColor];
view1StartPoint = view1.center;
view2StartPoint = view2.center;
lable1StartPoint = label1.center;
lable2StartPoint = label2.center;
[self resetEverything:nil];
}

- (IBAction)animateViews:(id)sender
{
[UIView animateWithDuration:duration
animations:^{
[view1 setBackgroundColor:highlightedColor]; // animates correctly
// [view1 setAlpha:0.2]; // animates correctly
CGPoint p = view1.center;
p.y -= yOffset;
view1.center = p; // animates correctly
}
completion:^(BOOL finished){
[UIView animateWithDuration:duration
animations:^{
[view2 setBackgroundColor:highlightedColor];
// [view2 setAlpha:0.2];
CGPoint p = view2.center;
p.y -= yOffset;
view2.center = p; // animates correctly
}
completion:nil];
}
];
}

- (IBAction)animateLabelsSimple:(id)sender // does NOT works sequentially
{
[UIView animateWithDuration:duration
animations:^{
[label1 setBackgroundColor:highlightedColor]; // changes instantly
// [label1 setTextColor:[UIColor orangeColor]]; // changes instantly
// [label1 setAlpha:0.2]; // animates correctly
CGPoint p = label1.center;
p.y -= yOffset;
label1.center = p; // animates correctly
}
completion:^(BOOL finished){
[UIView animateWithDuration:duration
animations:^{
[label2 setBackgroundColor:highlightedColor];
// [label2 setTextColor:[UIColor orangeColor]];
// [label2 setAlpha:0.2];
CGPoint p = label2.center;
p.y -= yOffset;
label2.center = p;
}
completion:nil];
}
];
}

- (IBAction)resetEverything:(id)sender
{
[view1 setBackgroundColor:normalColor];
[view2 setBackgroundColor:normalColor];
[label1 setBackgroundColor:normalColor];
[label2 setBackgroundColor:normalColor];

[view1 setAlpha:1.0];
[view2 setAlpha:1.0];
[label1 setAlpha:1.0];
[label2 setAlpha:1.0];

[label1 setTextColor:[UIColor blackColor]];
[label2 setTextColor:[UIColor blackColor]];

view1.center = view1StartPoint;
view2.center = view2StartPoint;
label1.center = lable1StartPoint;
label2.center = lable2StartPoint;
}

我查看了GitHub Chameleon项目,并了解了如何实现UIView类方法“animateWithDuration”:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
{
[self _beginAnimationsWithOptions:options | UIViewAnimationOptionTransitionNone];
[self setAnimationDuration:duration];
[self setAnimationDelay:delay];
[self _setAnimationCompletionBlock:completion];

animations();

[self commitAnimations];
}

那么,我是否必须继承UILabel并重写animateWithDuration方法?或最好的方法是什么?

最佳答案

您可以尝试使用https://stackoverflow.com/a/20892927/4030971通过动画更改uilabel的颜色。上面您已经实现了此功能,但需要进行一些更改。也可以尝试一下

[UIView animateWithDuration:duration animations:^{
[view1 setBackgroundColor:highlightedColor];
[view1 setAlpha:0.2];
[label setAlpha:0.0];
CGPoint p = view1.center;
p.y -= yOffset;
view1.center = p;
}completion:^(BOOL finished){
if(finished){
[label setBackgroundColor:[UIColor redColor]];
[UIView animateWithDuration:duration animations:^{
[view2 setBackgroundColor:highlightedColor];
[view2 setAlpha:0.2];
[label setAlpha:1.0];
CGPoint p = view2.center;
p.y -= yOffset;
view2.center = p;
}completion:nil];
}];

您可以在Alpha为0时编写标签颜色更改代码,然后可以通过使Alpha 1来为标签设置动画。颜色更改代码将在“是否完成”检查之后和“下一个动画”开始块之前进行。

关于ios - UIView按预期用animateWithDuration对setBackgroundColor进行动画处理。但是,UILabel立即发生。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784983/

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