2010-08-09 173 views
0

我將最終得到一個RSS源數組,並希望標籤或某些標籤在視圖的底部顯示它們。我想通過數組中的每個Feed進行動畫製作。動畫UILabel淡入/淡出

這是我到目前爲止的動畫,其中,爲淡入淡出,但只動畫數組的最後一項。

feed = [[UILabel alloc] initWithFrame:CGRectMake(0,380,320,43)]; 
[self.view addSubview:feed]; 

feed.alpha=1; 

NSArray *feeds = [NSArray arrayWithObjects:[NSString stringWithFormat:@"1234567"],[NSString stringWithFormat:@"qwerty"],[NSString stringWithFormat:@"asdfgh"],nil]; 

for (NSString* f in feeds){ 

    feed.text=f; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:2.0f]; 
    feed.alpha=0; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
    [UIView commitAnimations]; 

} 

我確定它很簡單。

謝謝

回答

7

首先,你應該真的考慮一個更好的命名約定。調用UILabel a feed對於未來不太有用,因爲您必須回過頭來查看代碼。我將其命名爲feedLabel。然後,當您遍歷您的Feed列表時,您可以僅使用for (NSString *feed in feeds),這將更有意義。 feedLabel.text = feed;也是如此。

無論如何,我在代碼中看到的問題是,您的循環中將alpha重複設置爲零,但您從未將它重新設置爲1。換句話說,你並沒有改變alpha值。它在每次迭代中都保持不變。

所以,也許你可以澄清你想要做的事情。如果您想淡化文字變化之間的文字,則需要使用不同的動畫和方法。而不是一個循環,鏈接你的動畫,當你的didStopSelector,你設置文本,並開始下一個。類似於:

- (void)performAnimation; 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:2.0f]; 
    feed.alpha=0; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)]; 
    [UIView commitAnimations]; 
} 

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag 
{ 
    feed.alpha = 1.0; 
    NSString *nextFeed = [self getNextFeed]; // Need to implement getNextFeed 
    if (nextFeed) 
    { 
    // Only continue if there is a next feed. 
    [feed setText:nextFeed]; 
    [self performAnimation]; 
    } 
} 
0

我試過了你的代碼,它在第一個feed上淡出,但它沒有輸入animationDidStop事件。這就是爲什麼它不能再次調用performAnimation。有沒有設置動畫(代表或協議等)。

+1

你需要調用[UIView setAnimationDelegate:self]; – joec 2010-09-20 15:24:13

+0

joec,你的帖子是正確的答案。您應該將其作爲答案而不是評論發佈,以便獲得相應的評價。 – 2012-05-17 23:41:05