Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: simple animation

I want to create a simple animation changing alpha value:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[view1 setAlpha:0.00];
[UIView commitAnimations];

Ok in this way I change the alpha value for two seconds and it works fine, but I want this thing: When alpha is 0 the animation must should start another time to alpha = 1 So the animation should be: alpha 0 -> alpha 1 -> alpha 0 -> alpha 1 -> ... and so on with a duration of two seconds. Can you help me?

my complete code is

-(IBAction){
FirstViewController *firstViewController = [[[FirstViewController alloc] init]autorelease];
[firstViewController.label1 setAlpha:1.00];
[firstViewController.label2 setAlpha:1.00];
view = firstViewController.viewFirst; //this is my view and I copy in other view
[self fadeOut:nil finished:nil context:nil];}

- (void) fadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView  setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeIn:finished:context:) ];
[view setAlpha:0.00];
[UIView commitAnimations];
}

- (void) fadeIn:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
[UIView  setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(fadeOut:finished:context:) ];
[view setAlpha:1.00];
[UIView commitAnimations];
}
like image 445
cyclingIsBetter Avatar asked Jun 17 '11 10:06

cyclingIsBetter


People also ask

Can you animate on IOS?

In the Messages app , you can animate a single message with a bubble effect or fill the entire message screen with a full-screen effect (for example, balloons or confetti).

Does Apple have animation software?

MotionSimply special effects. Motion is the powerful motion graphics tool that makes it easy to create cinematic 2D, 3D, and 360° titles, fluid transitions, and realistic effects in real time.


2 Answers

In iOS 4 and later you can do

   view1.alpha = 1.0;
   [UIView animateWithDuration:2.0
                          delay:0.0
                        options:UIViewAnimationOptionRepeat|UIViewAnimationAutoReverse
                     animations:^{
                        view1.alpha = 0.0;
                     }
                     completion:nil
   ];
like image 66
Pieter Avatar answered Sep 21 '22 00:09

Pieter


- (void) fadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];
    [UIView  setAnimationDelegate:self];
    if(animationRunning){
        [UIView setAnimationDidStopSelector:@selector(fadeIn:finished:context:) ];
    }
    [view1 setAlpha:0.00];
    [UIView commitAnimations];
}

- (void) fadeIn:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];
    [UIView  setAnimationDelegate:self];
    if(animationRunning){
        [UIView setAnimationDidStopSelector:@selector(fadeOut:finished:context:) ];
    }
    [view1 setAlpha:1.00];
    [UIView commitAnimations];
}

These will call each other on the end of the animation...

All you need to do to start the ball rolling is call:

[self fadeOut:nil finished:nil context:nil];
like image 38
Thomas Clayson Avatar answered Sep 20 '22 00:09

Thomas Clayson