递归 animateWithDuration:动画:完成:方法 iOS

Recursive animateWithDuration: animations: completion: method iOS

我正在尝试让我的项目 "glow" 每 3 秒将其 alpha 从高值更改为低值并返回。然而,它 "flickers" 并且基本上每 0.2 秒从一种颜色随机变化到另一种颜色。我已经粘贴了下面的方法。它可能与何时调用完成有关但不确定?

-(void)showLoading{
    [self.postTableView setHidden:YES];
    self.isLoading = true;
    //Disappear
    [UIView animateWithDuration:1.5 animations:^(void) {
        self.loadingLabel.alpha = 0.8;
        self.loadingLabel.alpha = 0.3;
    }
    completion:^(BOOL finished){
            //Appear
            [UIView animateWithDuration:1.5 animations:^(void) {
            self.loadingLabel.alpha = 0.3;
            self.loadingLabel.alpha = 0.8;
            }completion:^(BOOL finished2){
                if(true){
                    [self showLoading];
                }
            }];
    }]; }
-(void)viewDidLoad{
//use schedule timer to call repeatedly showLoading method until loading done
}
-(void)showLoading{
    [self.postTableView setHidden:YES];
    self.isLoading = true;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.loadingLabel.alpha = 0.3;
    [UIView commitAnimations];


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    self.loadingLabel.alpha = 0.8;
    [UIView commitAnimations];
}
self.loadingLabel.alpha = .8;
[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
    self.loadingLabel.alpha = .3;
} completion:nil];

UIViewAnimationOptionAutoreverse & UIViewAnimationOptionRepeat 可以帮到你。

你可以通过

来阻止它
self.loadingLabel.alpha = .8;

或其他动画。

[UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    self.loadingLabel.alpha = .8;
} completion:nil];

UIViewAnimationOptionBeginFromCurrentState 是一个有用的选项,可让您通过其他动画停止动画。