gpt4 book ai didi

ios - UIView 动画在 for 循环中的运行顺序不正确 - iOS

转载 作者:行者123 更新时间:2023-11-29 12:45:17 26 4
gpt4 key购买 nike

我有一个简单的 for 循环,可以为一些 UILabel 设置动画。我的 for 循环中有动画 block 代码的原因是因为显然我有多个标签要制作动画。

现在,每次整个 for 循环完成后,我的程序都会将 UILabel 中的数字递增 1。然而,当动画发生时,数字已经递增(这不是我想要的),即使数字递增代码出现在 for 循环之后......

我不确定我是否已经正确解释了问题,但考虑到我的问题,因为我们知道动画在主线程上运行的事实,我们如何确保动画以正确的顺序发生(在 for 循环中)???

这是我的代码:

for (int loop = 0; loop < [number_arrange count]; loop++) {

if ([number_arrange[loop] integerValue] == checknum) {
[anim_check replaceObjectAtIndex:loop withObject:[NSNumber numberWithInteger:1]];

[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(5.0, 5.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, -20.0);
((UILabel*)_labels_anim[loop]).alpha = 0.0;

} completion:^(BOOL finished) {

((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(1.0, 1.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, 20.0);
((UILabel*)_labels_anim[loop]).alpha = 1.0;
}];
}
}

谢谢你抽出时间,丹。

最佳答案

问题是您正在从两个 block 内部访问“循环”。为什么不尝试在动画 block 中运行循环?像这样:

[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
for (int loop = 0; loop < [number_arrange count]; loop++) {
if ([number_arrange[loop] integerValue] == checknum) {
[anim_check replaceObjectAtIndex:loop withObject:[NSNumber numberWithInteger:1]];

((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(5.0, 5.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, -20.0);
((UILabel*)_labels_anim[loop]).alpha = 0.0;
}
}
} completion:^(BOOL finished) {
for (int loop = 0; loop < [number_arrange count]; loop++) {
if ([number_arrange[loop] integerValue] == checknum) {
((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(1.0, 1.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, 20.0);
((UILabel*)_labels_anim[loop]).alpha = 1.0;
}
}
}];

希望对您有所帮助!

关于ios - UIView 动画在 for 循环中的运行顺序不正确 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23813625/

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