如何淡入和淡出标签.一直

use*_*440 1 objective-c

在我的Xcode项目中,我在xib上放了一个标签.

我想连续淡入和淡出标签,直到用户点击屏幕.当发生这种情况时,我希望出现一个新视图.

任何人都可以建议如何进行淡入/淡出?

Ryd*_*kay 8

您可以使用选项对UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat告诉Core Animation您希望标签连续淡入和淡出,而不是嵌套块并手动重新启动动画.

- (void)startAnimatingLabel
{
    self.label.alpha = 0;

    [UIView animateWithDuration:1
                          delay:0
                        options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                     animations:^{
                         self.label.alpha = 1;
                     } completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

要停止运行动画,只需从标签的图层中删除它们即可.

- (IBAction)tap:(UITapGestureRecognizer *)sender
{
    [self.label.layer removeAllAnimations];
    self.label.alpha = 0;

    [self presentNewView];
}
Run Code Online (Sandbox Code Playgroud)

编辑:一个不太突然的完成方式是从当前视图状态动画到最后一个(这将中断当前,重复动画).

- (IBAction)tap:(UITapGestureRecognizer *)sender
{
    [UIView animateWithDuration:1
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         self.label.alpha = 0;
                   } completion:^(BOOL finished){
                         [self presentNewView];
                   }];
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*ani 5

您可以在循环中放置一对链式动画,或者每次调用一个包含链式动画的函数,直到遇到用户点击.

通过链式动画,我的意思是这样的(您可以设置动画持续时间以满足您的需要):

myLabel.alpha = 0.0;
[UIView animateWithDuration:1.0
                      delay:0.0
                    options: UIViewAnimationCurveEaseOut
                 animations:^{
                     myLabel.alpha = 1.0;
                 } 
                 completion:^(BOOL finished){
                      [UIView animateWithDuration:1.0
                                            delay:1.0
                                           options: UIViewAnimationCurveEaseOut
                              animations:^{
                                     myLabel.alpha = 0.0;

                              }  
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                         }];
                 }];
Run Code Online (Sandbox Code Playgroud)

上面的代码将首先淡出您的标签,然后将其淡出.你可以把它放在一个函数中并调用它,直到你遇到用户点击.