动画UILabel淡入/淡出

joe*_*oec 0 iphone core-animation uiview uiviewanimation

我将最终得到一系列RSS提要,并希望在视图的底部显示标签或其他类似标签.我想为数组中的每个feed设置动画.

这是我到目前为止动画的内容,它适用于淡入淡出,但只能动画数组的最后一项.

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

}
Run Code Online (Sandbox Code Playgroud)

我确定它很简单.

谢谢

Mat*_*ong 7

首先,您应该考虑更好的命名约定.调用一个UILabel一个饲料时,你必须回来看看你的代码是不是对未来非常有帮助.我将它命名为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];
  }
}
Run Code Online (Sandbox Code Playgroud)