2010-08-09 137 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