views:

541

answers:

5

Hello, I would like to have an UIImageView that flickers. I thougt I can make it with CoreAnimation and the alpha-value. I tried this:

for (int a = 1; a <= 100; a++) {
    schwarz.alpha = 0.7;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:1];
    schwarz.alpha = 0,1;
    [UIView commitAnimations];
}

but it doenst work. He just moves to 0.1 and not again to 0.7. I also tried this:

schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 0.1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 1;
[UIView commitAnimations];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
schwarz.alpha = 3;
[UIView commitAnimations];
// and so on...

And again It doenst work. How can I implement the flickering? Thanks!

A: 

Use the first code sample posted, but take out the for statement and add the lines

[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:(float)number];

I think -1 for the number should be repeating indefinitely, but I'm not 100% sure on that.

Ian Henry
Thanks, that works, but how can I get a different animation after this with the same UIImageView. For exemple now he just moves from alpha 0.7 to 0.1, but I want after this an other animation.
Flocked
+1  A: 

All UIView animations added in one event loop are basically merged together. You need to use the UIView animationDidStopSelector. As an example, see the following:

-(void)tileAnimate:(NSString*)animationID finished:(BOOL)finished context:(void*)context  {
    int position = [animationID intValue];
    NSString *next = [NSString stringWithFormat:@"%d", position+1];
    if(position == boardSize) {
        return;
    }
    [UIView beginAnimations:next context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:timing];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(tileAnimate:finished:context:)];
    buttons[position].transform = CGAffineTransformIdentity;
    [UIView commitAnimations]; 
}

I use this to, one after another, animate shrinking an array of buttons back to their normal sizes.

David Kanarek
+2  A: 

The problem with your code is that the [UIView commitAnimation] method doesn't block - I mean that the code implementation continues and the animation is done asynchronously. So, actually, what is going on is that you go through all the loop iterations first and then the animation is done from 0.7 to 1.0...

Just use the setAnimationDidStopSelector without the "for" loop.

schwarz.alpha = 0.7;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
schwarz.alpha = 0.1;
[UIView commitAnimations];

The catching method might be:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
/* Do your things here... */ }
Michael Kessler
+1  A: 

Two more things:

First, UIViewAnimationCurveEaseIn does not work fading i think.

Second, in your original code you say:

schwarz.alpha = 0,1;

Note that there is a comma there and not a dot. Interestingly that code compiles, but it probably does not do what you intended.

St3fan
http://en.wikipedia.org/wiki/Comma_operator and you're right, it assigns alpha the value 1.
David Kanarek
+1  A: 

Here is another take on this. I'm not using fading but I think the flickering effect is still nice.

http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/Flicker/

St3fan
Thanks! :) I will take a look into the files.
Flocked