2010-01-24 53 views
0

我想有一個UIImageView閃爍。我想你可以用CoreAnimation和alpha值來製作它。我試過這個:想要閃爍UIImageView(CoreAnimation和alpha值)

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

但它可以工作。他只是移動到0.1,而不是0.7。 我也試過這樣:

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... 

又一次它doenst工作。我怎樣才能實現閃爍? 謝謝!

回答

2

你的代碼的問題是[UIView commitAnimation]方法不會阻塞 - 我的意思是代碼實現繼續,動畫是異步完成的。 所以,實際上,發生的事情是,首先完成所有循環迭代,然後將動畫從0.7更改爲1.0 ...

只需使用setAnimationDidStopSelector而不使用「for」循環。

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]; 

的捕捉方法可能是:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
/* Do your things here... */ } 
0

使用發佈第一個代碼示例,而是採取了for聲明,並添加行

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

我認爲-1的數量應該無限期地重複,但我不上肯定100% 。

+0

謝謝,這工作,但我怎麼能得到這個後一個不同的動畫具有相同的UIImageView。例如,現在他只是從阿爾法0.7移動到0.1,但我希望在這之後有另一個動畫。 – Flocked 2010-01-24 01:35:48

1

在一個事件循環中添加的所有UIView動畫基本上合併在一起。您需要使用UIView animationDidStopSelector。作爲一個例子,請參見:

-(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]; 
} 

我使用它來,一個接一個,動畫縮小按鈕陣列恢復到正常大小。

1

兩件事:

首先,UIViewAnimationCurveEaseIn不起作用衰落,我認爲。

其次,在原始的代碼你說:

schwarz.alpha = 0,1; 

注意,這裏面就有一個逗號,而不是一個點。有趣的是,代碼編譯,但它可能不會做你想要的。

+0

http://en.wikipedia.org/wiki/Comma_o​​perator,你是對的,它賦值爲1. – 2010-01-24 04:01:04