iOS连续淡出和淡入动画

Vig*_*gor 0 animation objective-c ios

objective-c,我想添加淡入淡出和淡出动画到UIView.我想连续做淡出和淡入动画.为了说明一下,我想要的效果如下:

淡出 - 淡入 - 淡出 - 淡入 - ......

我正在尝试的是如下:

-(void)fadeOutIn:(UIView *)view duration:(NSTimeInterval)duration
{
    static CGFloat alpha = 1.0;
    [UIView animateWithDuration:duration
                          delay:0.0
                        options: UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         alpha = abs((int)(alpha - 1.0));
                         view.alpha = alpha;
                     }
                     completion:^(BOOL finished){
                     }];
}
Run Code Online (Sandbox Code Playgroud)

我也试过以下:

-(void)fadeOutIn:(UIView *)view duration:(NSTimeInterval)duration
{
    [UIView animateWithDuration:duration
                          delay:0.0
                        options: UIViewAnimationOptionRepeat| UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         view.alpha = 0.0;
                     }
                     completion:^(BOOL finished){

                         [UIView animateWithDuration:duration
                                               delay:0.0
                                             options: UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              view.alpha = 1.0;
                                          }
                                          completion:^(BOOL finished){

                                          }];
                     }];
}
Run Code Online (Sandbox Code Playgroud)

但问题是他们的工作方式完全相同,即UIView意志淡出并且突然出现(不淡入),然后再次消失......

似乎动画中的淡入淡出根本不起作用.有人知道我的代码有什么问题吗?任何建议将受到高度赞赏.

Sah*_*Roy 11

试试这个

    [UIView animateKeyframesWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut animations:^{
    self.customIV.alpha = 0;
} completion:^(BOOL finished) {
}];
Run Code Online (Sandbox Code Playgroud)