2011-06-14 82 views
6

我正在嘗試動畫UIlabel首先變大,然後縮回原始框架。按預期擴大工作,但不縮小。當我用下面的代碼縮小標籤時,尺寸會在原點移動之前先進行調整。這會導致兩步動畫不平滑。動畫UILabel不光滑

這裏是我的代碼:

CGRect rect = label.frame; 
[UIView animateWithDuration:.2 
         delay: 0.1 
        options: UIViewAnimationOptionBeginFromCurrentState 
       animations:^{ 
        label.frame = CGRectMake(rect.origin.x + 4, 
                     rect.origin.y + 4, 
                     rect.size.width-8, 
                     rect.size.height-8); 
       } 
       completion:^(BOOL finished){ 
       }]; 
+0

你不是做一個過渡。你爲什麼使用'UIViewAnimationOptionTransitionNone'? – 2011-06-14 01:04:02

+0

不要忘了給予賞金:) – jmosesman 2011-07-02 05:32:34

回答

2

你可以嘗試將變換應用於標籤動畫塊內,而不是調整RECT。像下面的線,爲成長的東西/收縮動畫:

label.transform = CGAffineTransformMakeScale(1.5, 1.5); //grow 
label.transform = CGAffineTransformMakeScale(1, 1);  //shrink 
2

請試訓的這個解決方案,我想這是你在找什麼。我已經測試過它的工作,但請嘗試讓我知道,如果你正在尋找這個或沒有。

-(IBAction)growanimate:(id)sender 
{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(shrinkanimate)]; 
    label.transform = CGAffineTransformMakeScale(2.0f, 2.0f); //This will double label from current size. 
    [UIView commitAnimations]; 
} 

-(void)shrinkanimate 
{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 
    label.transform = CGAffineTransformMakeScale(1.0f, 1.0f); //This will get it back to original size. 
    [UIView commitAnimations]; 
} 
+0

thx ..對於更詳細的版本,但得到了更早的答案 – prostock 2011-06-30 21:29:17

1

我想這個問題是由調整幀大小觸發調整大小通知的事實引起的。縮小時可能會有更多的中斷,因爲超級視圖的新區域被揭示。

在這種情況下,變換方法要好得多。

我還猜測,使用變換方法時,字形路徑和佈局(換行符等)不會重新計算,並且緩存的CGPath只在渲染時進行轉換。


關於居中問題,我沒有看到任何問題。

我想補充一點,如果你打算廣泛使用這個效果,你可以爲這個效果創建一個靜態類,或者包含幾個預置動畫,裏面有靜態存儲的變換。

然後你打電話這樣[MyPopEffect popView:mylabel];

它避免了創建和釋放轉換,並允許即時使用任何視圖或其他項目。

不管怎麼說,這是動畫代碼...乾杯

[

UIView animateWithDuration:0.5f delay: 0.0f 
    options:UIViewAnimationOptionCurveEaseOut+UIViewAnimationOptionBeginFromCurrentState 
     animations:^{ 
      label.transform=CGAffineTransformMakeScale(2.0f,2.0f); 
     } 
    completion:^(BOOL finished){ 
      [UIView animateWithDuration:0.5f delay: 0.0f 
      options:UIViewAnimationOptionCurveEaseIn+UIViewAnimationOptionBeginFromCurrentState 
      animations:^{ 
       label.transform=CGAffineTransformMakeScale(1.0f,1.0f); 
      } 
      completion:^(BOOL finished){}]; 
    }]; 

]