动画UILabel淡入/淡出 [英] Animating UILabel Fade In/Out

查看:89
本文介绍了动画UILabel淡入/淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将最终获得一系列RSS feed,并希望在视图底部显示标签或类似标签。我想对数组中的每个提要进行动画处理。

I am going to end up with an array of RSS feeds, and would like a label or some such to display them at the bottom of the view. I would like to animate through each feed in the array.

这是我到目前为止要进行动画处理的内容,它可以淡入淡出,但仅对最后一项进行动画处理

This is what i have so far to animate, which, works for the fade, but only animates the last item of the array.

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

}

我相信这很简单。

谢谢

推荐答案

首先,您应该真正考虑一个更好的命名约定。将UILabel称为 feed 对于将来不得不返回查看代码的情况不是很有帮助。我将其命名为 feedLabel 。然后,当您遍历提要列表时,只需(NSString *提要中的提要),它将更有意义。因此 feedLabel.text = feed;

First off you should really consider a better naming convention. Calling a UILabel a feed isn't very helpful for the future when you have to come back and look at your code. I would name it feedLabel. Then when you iterate through your list of feeds, you can just for (NSString *feed in feeds) and it will make more sense. And so will feedLabel.text = feed;.

无论如何,我在您的代码中看到的问题是在您的循环中反复将alpha设置为零,但您从未将其重新设置为1。换句话说,您不需要更改Alpha值。每次迭代都保持不变。

Anyhow, the issue I see with your code is that your are repeatedly setting the alpha to zero in your loop, but you never set it back to one. In other words, you're not making a change to the alpha value. It stays the same in every iteration.

所以也许您可以弄清楚自己要做什么。如果要在文本更改之间淡入淡出文本,则需要使用其他动画和方法。而不是循环,而是链接动画,以便在didStopSelector设置文本并开始下一个动画。

So maybe you could clarify what you're trying to do. If you want to fade the text between changes in the text, you'll need a different animation and methodology. Instead of a loop, chain your animations such that when your didStopSelector, you set the text and start the next one. Something like:

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

这篇关于动画UILabel淡入/淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆