2011-08-10 60 views
2

我有一個圖像「image1」這是一個UIButton,我想要的是,如果我觸摸此按鈕,圖像的寬度乘以2(與動畫)不是高度只是寬度。 對不起,我的英語我是法國人:/用動畫修改imageView的寬度

回答

3

這是相當容易的塊動畫。只需嘗試:

imageView.contentMode = UIViewContentModeScaleAspectFit; 

[UIView animateWithDuration:1.0 
         animations:^{ 
         CGRect newRect = imageView.frame; 
         int movementToTheLeft = imageView.frame.size.width/2; 
         newRect.size.width *= 2; 
         newRect.origin.x -= movementToTheLeft; 
         imageView.frame = newRect; 
         } 
         completion:^(BOOL finished){ 

         [UIView animateWithDuration:1.0 
              animations:^{ 
              NSLog(@"Scaled!");// Anything can go in this completion block, it should be used for any logic you need after the animation. 
              } 
              completion:^(BOOL finished){ 
              ; 
              }]; 

這應該縮放您的imageView的寬度2。希望有所幫助!

+0

很好的回答除了在完成塊的第二animateWithDuration。你真的是這個意思嗎? –

+0

我通常使用它來擁有一個布爾變量,它可以阻止用戶按下按鈕,直到動畫完成。也可以觸發其他動畫或通知到您的代碼。你不需要它,這只是我的偏好。是的,1.0這個例子有點太大了! – msgambel

+0

這段代碼不起作用,因爲當我觸摸按鈕時,我的按鈕向右移動,其寬度不變,它只是向右移動 –

0

的老式方法(的iOS 4.0後不推薦使用,但仍然有效):

[UIView beginAnimations:@"widen" context:nil]; 
[UIView setAnimationDuration:1.0]; 
CGRect newRect = imageView.frame; 
newRect.width *= 2; 
imageView.frame = newRect; 
// ... set more parameters if desired 
[UIView commitAnimations];