2011-12-11 48 views
2

我有一個動畫,沿x軸略微反彈一個視圖。問題在於使用舊的動畫機制,並且由於文檔狀態建議在iOS4 +中使用基於塊的動畫。我不知道我怎樣才能把這個簡單的動畫到基於塊的動畫:將簡單的動畫轉換爲基於塊的動畫

[UIView beginAnimations:@"bounce" context:nil]; 
[UIView setAnimationRepeatCount:2]; 
[UIView setAnimationRepeatAutoreverses:YES]; 
self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y); 
[UIView commitAnimations]; 

而且我想視圖返回到動畫結束後,它的起始位置,以上方法反彈的觀點,並增加它的x值由10個像素。

感謝您的任何幫助。

回答

5

塊基地動畫的格式是這樣的:

[UIView animateWithDuration:<#(NSTimeInterval)#> 
         delay:<#(NSTimeInterval)#> 
        options:<#(UIViewAnimationOptions)#> 
       animations:<#^(void)animations#> 
       completion:<#^(BOOL finished)completion#>]; 

和下面的代碼反彈twicego back to origin position可能存在更好的方法的觀點:

[UIView animateWithDuration:0.2f 
         delay:0.0f 
        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y); 
       } 
       completion:^(BOOL finished) { 
        self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y); 
        [UIView animateWithDuration:0.2f 
              delay:0.0f 
             options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut 
             animations:^{ 
              self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y); 
             } 
             completion:^(BOOL finished) { 
              self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y); 
             }]; 
       }]; 

而這一次是相似的,但是go back to origin position not smoothly

[UIView animateWithDuration:0.2f 
         delay:0.0f 
        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y); 
        self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y); 
       } 
       completion:nil]; 
+0

謝謝@Kjuly您提供的中間代碼塊做了訣竅:) – bennythemink

+0

@bennythemink u'r welcome :) – Kjuly

0
 [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionAutoreverse     animations:^{ 
    self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y); 
    } completion:nil]; 

這應該這樣做,但我不認爲有一種方法來指定自動重複計數。

2

試試這個代碼:

[UIView setAnimationRepeatCount:2]; 
[UIView animateWithDuration:0.2 
         delay:0.2 
        options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) 
       animations:^{ 
        self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y); 
       } completion:^(BOOL finished) { 
}]; 

附:我還沒有嘗試過...

+0

謝謝阿里爾,但這會重複動畫永遠。我猜我可以放入一些'if'語句檢查,但@ Kjuly的答案讓我只用動畫塊來做。反正給了你一個投票給你的麻煩:) – bennythemink