2013-07-11 30 views
2

大家好我是xcode的新手,我嘗試通過更改圖像來觸摸動畫中的氣球:這是我的代碼: 現在我正面臨的問題它是不是動畫圖像意味着動畫計時器不工作:請指導我該怎麼做,以便隨着時間動畫圖像:如果我沒有做好它,請指導我,我怎麼能通過NSTimer做到這一點?如何在一段時間後通過更改圖像來創建動畫

-(void)baloonbursting:(UIButton *)button withEvent:(UIEvent *)event{ 
if ([[UIImage imageNamed:@"redbaloons.png"] isEqual:button.currentImage]) { 
    NSLog(@"em redbaloons.png"); 
    UIImage *bubbleImage3 = [UIImage imageNamed:@"redburst.png"]; 
    [button setImage:bubbleImage3 forState:UIControlStateNormal]; 
} 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView animateWithDuration:1.0f animations:^(){ 
    // define animation 
    if ([[UIImage imageNamed:@"redburst.png"] isEqual:button.currentImage]) { 
     NSLog(@"em redbaloons.png"); 
     UIImage *bubbleImage3 = [UIImage imageNamed:@"redburst2.png"]; 
     [button setImage:bubbleImage3 forState:UIControlStateNormal]; 
    } 
} 
completion:^(BOOL finished){ 
// after the animation is completed call showAnimation again 
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction animations:^{ 

        } completion:^(BOOL finished){ 
         if (finished) { 
          [button removeFromSuperview]; 
         }}]; 
       }]; 

}

+0

我不認爲UIButton的圖像屬性可以是動畫的。 – Andrew

+0

指導我如何通過NSTIMER做到這一點? – Steve

+0

爲什麼你在if中創建UIImage只是爲了檢查圖像是否相等,難道你不能僅僅使用圖像的'name'屬性? –

回答

2

我要你給你的解決方案向您展示正確的方向去思考。也就是說,爲什麼我在Xcode中開發了一個小型測試項目,並且我控制了這個代碼真正起作用。

首先:忘記此動畫的NSTimers!

這個想法是,你「玩」了子視圖,因爲你可以將它們的alpha屬性從0.0(不可見)改爲1.0(完全不透明),這是由SDK的視圖動畫支持的。

請根據您自己的文件名更改圖像名稱(我在這裏使用過我自己的)。

下面的方法檢查按鈕的圖像是否應該調用動畫 - 正是你之前做的。如果滿足這個條件,它可視化地將按鈕圖像的變化動畫化爲另一個圖像:

- (IBAction)balloonBursting:(UIButton *)sender 
{ 
    BOOL doAnimate = NO; 

    UIImageView *ivOldBubbleImage; 
    UIImageView *ivNewBubbleImage; 

    if ([[UIImage imageNamed:@"BalloonYellow.png"] isEqual:sender.currentImage]) { 
     NSLog(@"will animate"); 
     doAnimate = YES; 

     UIImage *newImage = [UIImage imageNamed:@"BalloonPurple.png"]; 
     ivNewBubbleImage = [[UIImageView alloc] initWithImage:newImage]; 
     ivNewBubbleImage.alpha = 0.0; 
     ivOldBubbleImage = sender.imageView; 
     [sender addSubview:ivNewBubbleImage]; 
    } 

    if (doAnimate) { 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
     [UIView animateWithDuration:1.0f animations:^(){ 
      // define animation 
      ivOldBubbleImage.alpha = 0.0; 
      ivNewBubbleImage.alpha = 1.0; 
     } 
         completion:^(BOOL finished){ 
          [sender setImage:ivNewBubbleImage.image forState:UIControlStateNormal]; 
          [ivNewBubbleImage removeFromSuperview]; 
         }]; 
    } 
} 
相關問題