开发者

Want a flickering UIImageView (CoreAnimation and alpha-value)

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 mov开发者_运维技巧es 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!


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... */ }


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.


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.


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/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜