我正在尝试使用UILabels淡入淡出来构建我的应用程序的介绍。我有两个标签。我希望第一个淡入,在屏幕上停留4秒钟。然后第二个标签应该淡入并在屏幕上停留4秒钟。然后它应该淡出两个标签。
我有以下代码但它没有做任何事情,因为它直接进入最终状态。我在viewDidAppear()中有以下方法。我做错了什么?
-(void) animateLabels
{
[UIView beginAnimations:@"First Label Display" context:nil];
[UIView setAnimationDelay:4.0];
firstLabel.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:@"Second Label Display" context:nil];
[UIView setAnimationDelay:6.0];
secondLabel.alpha = 1;
[UILabel commitAnimations];
[UIView beginAnimations:@"Hide Labels" context:nil];
[UIView setAnimationDelay:10.0];
secondLabel.alpha = 0;
firstLabel.alpha=0;
[UILabel commitAnimations];
}
答案 0 :(得分:3)
使用基于块的动画&将动画链接在一起。所以有3个步骤。 label1 fadesIn,Label2 fadesIn,最后Label3淡入。我已经在下面编写了代码,用于淡化label1和amp; LABEL2。淡出很简单。我想你可以填写剩下的部分。从这里直接前进......
试试这个 -
[UIView animateWithDuration:1.0
delay:4
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
[firstLabel setAlpha:1.0];
}
completion:^(BOOL finished)
{
if(finished)
{
[UIView animateWithDuration:1.0
delay:4.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
[secondLabel setAlpha:1.0];
}
completion:^(BOOL finished)
{
if(finished)
{
//put another block her to hide both the labels.
}
}];
}
}];
答案 1 :(得分:1)
我建议使用块重写它。首先animateWithDuration:animations:completion:
然后嵌套animateWithDuration:delay:options:animations:completion:
。它更加灵活,而且现在不需要在预阻塞系统上运行。
此外,你写的第一个动画不会触发4秒。
答案 2 :(得分:0)
这是 swift 4 +
中的解决方案 UIView.animate(withDuration: 1.0, delay: 4, options: [.curveLinear, .allowUserInteraction], animations: {
firstLabel.alpha = 1.0
}) { finished in
if finished {
UIView.animate(withDuration: 1.0, delay: 4.0, options: [.curveLinear, .allowUserInteraction], animations: {
secondLabel.alpha = 1.0
}) { finished in
if finished {
//put another block her to hide both the labels.
}
}
}
}