簡體   English   中英

UIView動畫在for循環中的運行順序不正確-iOS

[英]UIView Animation doesn't run in correct order in for loop - iOS

我有一個簡單的for循環,它對一些UILabel進行了動畫處理。 我的for循環中包含動畫塊代碼的原因是,顯然,我有多個要嘗試制作動畫的標簽。

現在,在整個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;
                }];
            }
        }

丹,謝謝您的時間。

問題是您要從兩個塊內部訪問“循環”。 為什么不嘗試在動畫塊內部運行循環? 像這樣:

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

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM