动画的UILabel淡入/淡出
-
27-09-2019 - |
题
我将与RSS源的阵列,以结束,并希望的标签或一些这样的视图的底部以显示它们。我想通过动画阵列中的每个进料。
这是我迄今为止的动画,其中,工程褪色,但只有动画数组的最后一个项目。
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];
}
Im肯定它的简单。
由于
解决方案
首先,你应该真正考虑一个更好的命名约定。调用一个UILabel一个的饲料的时候,你必须回来看看你的代码是不是对未来非常有帮助。我将它命名为 feedLabel 的。然后,当你通过迭代的饲料的列表,你可以for (NSString *feed in feeds)
,它会更有意义。等会feedLabel.text = feed;
。
总之,我与你的代码中看到的问题是,你反复在循环阿尔法设置为零,但你永远不设置回一个。换句话说,你不做出改变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];
}
}
其他提示
我想你的代码,它淡出的第一进料,但它并没有进入animationDidStop事件。这就是为什么它不能再打电话performAnimation。是否有任何组的动画(委托或协议等。)
不隶属于 StackOverflow