Jean Lebrument
Jean Lebrument

Reputation: 5169

Repeat Fade In / Fade Out effects to switch between three views

I've created one view containing three subviews.

I would like to fade in / out the three subviews and repeat the effect indefinitely.

I try something like :

+ (void)fadeIn:(UIView *)view withDuration:(float)duration
{
    [UIView animateWithDuration:duration animations:^{
        view.alpha = 1.0;
    }];
}

+ (void)fadeOut:(UIView *)view withDuration:(float)duration
{
    [UIView animateWithDuration:duration animations:^{
        view.alpha = 0.0;
    }];
}

[UIView animateWithDuration:5.0
                      delay:2.0
                    options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^
{
    [Utilities fadeOut:headSubtitle_1 withDuration:1];
    [Utilities fadeIn:headSubtitle_2 withDuration:1];
    [Utilities fadeOut:headSubtitle_2 withDuration:1];
    [Utilities fadeIn:headSubtitle_3 withDuration:1];
    [Utilities fadeOut:headSubtitle_3 withDuration:1];
    [Utilities fadeIn:headSubtitle_1 withDuration:1];
}
                 completion:^(BOOL finished)
{}];

But it doesn't work well.

The effect that I would obtain is :

  1. Display headSubtitle_1 for 2 seconds
  2. FadeOut headSubtitle_1 in 1 second
  3. FadeIn headSubstitle_2 in 1 second
  4. Display headSubstitle_2 for 2 seconds
  5. FadeOut headSubtitle_2 in 1 second
  6. FadeIn headSubstitle_3 in 1 second
  7. Display headSubstitle_3 for 2 seconds
  8. FadeOut headSubtitle_3 in 1 second
  9. FadeIn headSubtitle_1 in 1 second
  10. Repeat to 1.

Thanks a lot !

EDIT

Finally, I try something like this :

+ (void)runAnimationSubtitleOrdersWithSub1:(UITextView *)headSubtitle_1 andSub2:(UITextView *)headSubtitle_2 andSub3:(UITextView *)headSubtitle_3
{
    [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        headSubtitle_1.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished)
        {
            [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                headSubtitle_2.alpha = 1;
            } completion:^(BOOL finished) {
                if (finished)
                {
                    [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                        headSubtitle_2.alpha = 0;
                    } completion:^(BOOL finished) {
                        if (finished)
                        {
                            [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                                headSubtitle_3.alpha = 1;
                            } completion:^(BOOL finished) {
                                if (finished)
                                {
                                    [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                                        headSubtitle_3.alpha = 0;
                                    } completion:^(BOOL finished) {
                                        if (finished)
                                        {
                                            [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                                                headSubtitle_1.alpha = 1;
                                            } completion:^(BOOL finished) {
                                                [FormBuilder runAnimationSubtitleOrdersWithSub1:headSubtitle_1 andSub2:headSubtitle_2 andSub3:headSubtitle_3];
                                            }];
                                        }
                                    }];
                                }
                            }];
                        }
                    }];
                }
            }];
        }
    }];
}

It's really ugly ! If some body knows a better way to do this.

Upvotes: 1

Views: 450

Answers (2)

Jordan Montel
Jordan Montel

Reputation: 8247

Maybe you can play with several animation with something like this (but, it's not beautiful) :

_imageView.alpha = 0.0f;
_imageView2.alpha = 0.0f;
_imageView3.alpha = 0.0f;


[UIView animateWithDuration:1
                      delay:0
                    options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                 animations:^{

                     _imageView.alpha = 1.0f;

                     [UIView animateWithDuration:1
                                           delay:1
                                         options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
                                      animations:^{

                                          _imageView2.alpha = 1.0f;

                                      } completion:^(BOOL finished) {

                                          _imageView2.alpha = 0.0f;

                                      }
                      ];


                 } completion:^(BOOL finished) {

                     _imageView.alpha = 0.0f;


                 }
 ];

Upvotes: 2

ilya n.
ilya n.

Reputation: 18816

Take a look at [UIView beginAnimations:(NSString *) context:(void *)].

Upvotes: 0

Related Questions