2014-02-19 31 views
2

我正在尋找一種方法來排隊的動畫塊,整個這篇博客文章發生了: http://xibxor.com/2013/03/27/uiview-animation-without-nested-hell/這個區塊隊列中發生了什麼?

我無法得到它的工作,雖然...如何安排這些元素的範圍不明確。另外,第18,25和32行的分號是什麼?任何人都可以解釋如何使用它?

編輯:這裏是從源複製的代碼:

NSMutableArray* animationBlocks = [NSMutableArray new]; 

typedef void(^animationBlock)(BOOL); 

// getNextAnimation 
// removes the first block in the queue and returns it 
animationBlock (^getNextAnimation)() = ^{ 
    animationBlock block = animationBlocks.count ? (animationBlock)[animationBlocks objectAtIndex:0] : nil; 
    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){; 
    [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); 
+1

據我看到後快速查看;錯字但不干擾;像空行... – Volker

回答

2

首先感謝分享這篇文章的,這是一個好主意。完美的作品對我來說:

在您的.h

@interface

typedef void(^animationBlock)(BOOL); 

然後在您的m(這裏面- (void)viewDidLoad):

NSMutableArray* animationBlocks = [NSMutableArray new]; 

UILabel* labelTest = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; 
labelTest.text = @"Label Test"; 

[self.view addSubview:labelTest]; 

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

//add a block to our queue 
[animationBlocks addObject:^(BOOL finished){; 
    [UIView animateWithDuration:2.0 animations:^{ 
     NSLog(@"1-step Animation Complete!"); 
     labelTest.alpha = 0; 
    } completion: getNextAnimation()]; 
}]; 

//add a block to our queue 
[animationBlocks addObject:^(BOOL finished){; 
    [UIView animateWithDuration:2.0 animations:^{ 
     labelTest.text = @"New text"; 
    } completion: getNextAnimation()]; 
}]; 

//add a block to our queue 
[animationBlocks addObject:^(BOOL finished){; 
    [UIView animateWithDuration:2.0 animations:^{ 
     labelTest.alpha = 1; 
    } 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); 

這是一個非常簡單的動畫,但你需要它來測試它^^

希望這會有所幫助。

+0

我的用例必須是問題。我有一個顯示標籤的UIView子類。我有一個函數(讓我們稱之爲changeLabel :),它使標籤淡出,改變文本,然後將標籤淡入。目標是能夠調用changeLabel:快速一堆,並且動畫不是互相打斷。 – GoldenJoe

+1

好吧,我編輯我的答案,你想要什麼。 –