簡體   English   中英

UIView iPhone SDK嵌套動畫

[英]UIView iPhone SDK nested animations

我想為我的視圖制作一個嵌套動畫。

我有一個動畫停止選擇器,它的名稱很好:

[UIView setAnimationDidStopSelector:@selector(growAnimationDidStop1:finished:context:)];

但是這個選擇器內部 ,我想做更多的動畫,完成后又要調用另一個選擇器:

- (void)growAnimationDidStop1:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{
...
    [UIView setAnimationDidStopSelector:@selector(growAnimationDidStop2:finished:context:)];
... 

    [UIView commitAnimations];
}

問題在於,從未調用growAnimationDidStop2 為什么是這樣?

您也可以使用blocks來執行此操作,Apple現在建議這樣做。 將以下內容視為偽代碼:

[UIView animateWithDuration:0.3
                        delay:0.0
                      options:UIViewAnimationCurveEaseInOut
                   animations:^{
                     [self doAnimationOne];
                   } completion:^(BOOL finished){
                     [UIView animateWithDuration:0.4
                                           delay:0.0
                                         options:UIViewAnimationCurveEaseInOut
                                      animations:^{
                                        [self doAnimationNine];
                                      }
                                      completion:^(BOOL finished) {
                                        [UIView animateWithDuration:0.3
                                                              delay:0.0
                                                            options:UIViewAnimationCurveEaseInOut
                                                         animations:^{
                                                           [self doAnimationFive];
                                                         } 
                                                         completion:^(BOOL finished) {}];
                                      }];
                   }];

將動畫設為“社交”也是一種好習慣,例如:

BOOL userInteractionEnabled = [self isUserInteractionEnabled];
[self setUserInteractionEnabled:NO];

BOOL animationsEnabled = [UIView areAnimationsEnabled];
[UIView setAnimationsEnabled:YES];

在開始動畫之前,然后在最終完成塊中執行以下操作:

[UIView setAnimationsEnabled:animationsEnabled];
[self setUserInteractionEnabled:userInteractionEnabled];

糟糕,我自己回答了。 我必須在第一種停止方法中啟動全新的動畫上下文

順序執行多個動畫的最佳方法是使用塊隊列。

看看結果有多干凈:

NSMutableArray* animationBlocks = [NSMutableArray new];

typedef void(^animationBlock)(BOOL);

// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
    animationBlock block = (animationBlock)[animationBlocks firstObject];
    if (block){
        [animationBlocks removeObjectAtIndex:0];
        return block;
    }else{
         return ^(BOOL finished){};
    }
};

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    [UIView animateWithDuration:1.0 animations:^{
        //...animation code...
    } completion: getNextAnimation()];
}];

//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
    NSLog(@"Multi-step Animation Complete!");
}];

// execute the first block in the queue
getNextAnimation()(YES);

摘自: http : //xibxor.com/objective-c/uiview-animation-without-nested-hell/

暫無
暫無

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

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