iOS连续淡出和淡入动画

时间:2015-08-17 06:19:07

标签: ios objective-c animation

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){
                     }];
}

我也尝试过:

-(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){

                                          }];
                     }];
}

但问题是它们的工作方式完全相同,即UIView会正常淡出而突然出现(不淡入),然后再次淡出...... < / p>

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

5 个答案:

答案 0 :(得分:11)

试试这个

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

答案 1 :(得分:1)

#!/usr/bin/python

import sys
import socket
import dns.resolver
import re


def getspf (domain):
   answers = dns.resolver.query(domain, 'TXT')
   for rdata in answers:
     for txt_string in rdata.strings:
       if txt_string.startswith('v=spf1'):
         return txt_string.replace('v=spf1','')

with open('Input_Domains.txt','r') as f:
     for line in f:
        full_spf=getspf(line.strip())
my_file=open("out_spf.txt","w")
my_file.write(full_spf)
my_file.close()

只需拨打一次

答案 2 :(得分:1)

这就是这个,Vigor,这确实有效,但我不得不修改代码,这是一个有点危险的代码,因为它不断递归,但你会明白这一点:

-(void)fadeOutIn
{
    [UIView animateWithDuration:1
                          delay:0.0
                        options:  UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         [[self contentView] emailField].alpha = 0.0;
                     }
                     completion:^(BOOL finished){

                         [UIView animateWithDuration:1
                                               delay:0.0
                                             options: UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              [[self contentView] emailField].alpha = 1.0;
                                          }
                                          completion:^(BOOL finished){
                                              [self fadeOutIn];

                                          }];
                     }];
}

我真的会重新考虑这是如何工作的,我在视图中的一个按钮上使用它,它会一直递响,直到视图从superview中删除。我不得不对代码中的按钮进行硬编码。

答案 3 :(得分:1)

试试这个......

YourView.alpha = 1;// set the initial value of alpha

[UIView animateWithDuration:1 delay:0
                            options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                         animations:^{
                                YourView.alpha = 0; // set the max value of alpha
                            } completion:nil];

答案 4 :(得分:0)

此解决方案适用于Swift 3,4和5

UIView.animateKeyframes(withDuration: 1, delay: 0, options: [UIView.KeyframeAnimationOptions(rawValue: UIView.AnimationOptions.repeat.rawValue), UIView.KeyframeAnimationOptions(rawValue: UIView.KeyframeAnimationOptions.RawValue(UIView.AnimationCurve.easeInOut.rawValue)), UIView.KeyframeAnimationOptions(rawValue: UIView.AnimationOptions.autoreverse.rawValue)] , animations: {
        self.logoCenterImageView.alpha = 0
    }) { finished in
    }
相关问题