2012-08-03 41 views
1

我試圖爲一個按鈕設置動畫。該按鈕將以0和0的高度和寬度開始並擴展到其預期大小的100%。然後它會縮小到其尺寸的80%,然後再回到100%。在沒有明確定義CGRect的情況下縮小一個框架80%

有沒有辦法做到這一點,而不必明確計算每個職位的CGRect?問題在於,在80%時,框架與100%框架的位置不完全對齊。我必須手動計算幀的x和y位置,這是很耗時間的,因爲我正在爲很多按鈕執行此操作。

這裏就是我目前正在做的事:

CGRect smallPlay = CGRectMake(PlayButton.frame.origin.x + 94, PlayButton.frame.origin.y + 23, 0, 0); 
    CGRect almostFullPlay = CGRectMake(40, 170, 160, 30); 

    [UIView animateWithDuration:0.3 
        delay:0 
        options: UIViewAnimationCurveEaseOut 
        animations:^{ 

        PlayButton.frame = smallPlay; 
        PlayButton.frame = CGRectMake(50, 178, 187, 45); 

       } 
       completion:^(BOOL finished){ 
        [UIView animateWithDuration:0.2 
         delay:0 
         options:UIViewAnimationCurveEaseOut 
         animations:^{ 

          PlayButton.frame = almostFullPlay; 
          PlayButton.frame = CGRectMake(50, 178,187, 45); 

        } completion:^(BOOL finished){ 
         NSLog(@"Done!"); 
        } 
         ]; 
       }]; 

編輯:我知道我可以寫一個函數在80%來計算的框架,但我想知道如果有一個更好的辦法這樣做不需要我額外寫任何東西。

回答

5

看起來你只是想暫時操縱按鈕的外觀。如果是這種情況,不要改變其frame,請嘗試轉換它的transform!這是一個起點。讓我知道你是否需要更多的理解。

[UIView 
    animateWithDuration: ... 
    delay:    ... 
    options:    UIViewAnimationCurveEaseOut 
    animations:   ^{ 
     PlayButton.transform = CGAffineTransformMakeScale(0.8, 0.8); 
    }]; 

要恢復按鈕到原來的變換(稱爲「身份轉換」),其transform屬性設置爲CGAffineTransformIdentity

一旦你得到了想通了,對CGAffineTransform讀了(石英錶哥NSAffineTransform,以及CATransform3D二維大伯父。)

+0

非常感謝!這正是我所期待的。 – 2012-08-03 03:26:53

相關問題