2013-06-26 63 views
1

在我的應用程序中,我想給一個動畫UIButtons,就像按鈕在屏幕上「隱藏」時一樣。在iOS中隱藏UIButton的動畫

我試過下面的代碼,但它沒有給我一個好的結果。

[UIView animateWithDuration:1.5 
       animations:^{ 
        S1Button.frame = CGRectMake(20, 10, 50, 10); 
       }]; 
[S1Button setHidden:YES]; 
break; 
+0

您是否希望在下降時看到按鈕? – AtWork

回答

5

您可以設置新的位置和隱藏動畫後的按鈕。

[UIView animateWithDuration:0.9 animations:^{ 
     tradeButton.frame = (CGRect){ CGPointMake(51, 150), tradeButton.bounds.size }; 
    } completion:^(BOOL finished) { 
     tradeButton.hidden = YES; 
     // etc. 
    }]; 
+0

它爲我工作 – posha

+0

哦! good..Enjoy !!!! :) – AtWork

+0

@kevin不要忘記接受答案。 – AtWork

4

使用具有完成塊的動畫方法並在其中隱藏按鈕。目前,您的隱藏方法會立即運行,因此您看不到動畫。

4
[UIView animateWithDuration:1 animations:^{ 
     S1Button.frame = CGRectMake(20, 10, 50, 10); 
    } completion:^(BOOL finished) { 
     [S1Button setHidden:YES]; 
    }] 
1
[UIView animateWithDuration:0.25f 
       animations:^{ 
        S1Button.frame = CGRectMake(20, 10, 50, 10); 
       }completion:^(BOOL completed){ 
        [UIView beginAnimations:nil context:nil]; 
        [UIView setAnimationDuration:.3]; 

        S1Button.alpha = 1; 
        [UIView commitAnimations]; 

    }];  
1

試試這個

To fade out: 

     [UIView animateWithDuration:0.3 animations:^{ 
       button.alpha = 0; 
      } completion: ^(BOOL finished) { 
       button.hidden = YES; 
      }]; 


     To fade in: 


      button.alpha = 0; 
      button.hidden = NO; 
      [UIView animateWithDuration:0.3 animations:^{ 
       button.alpha = 1; 
      }]; 
0

試試這個:

您在動畫完成之前隱藏按鈕,沒有動畫是可見的。所以,用這種替換代碼:

[UIView animateWithDuration:1.5 animations:^{ 
    S1Button.frame = CGRectMake(20, 10, 50, 10); 
} completion:^(BOOL finished) { 
    [S1Button setHidden:YES]; 
}]; 

break; 
1

在你的代碼,按鈕的隱藏屬性不是動畫。當此動畫塊運行時,您的按鈕將立即隱藏,但不會淡入淡出/動畫。淡出UIView的適當方法是將其alpha屬性從1.0移動到0.0,如下所示:

[UIView animateWithDuration:2.0 
          delay:0.0 
         options: UIViewAnimationCurveEaseOut 
        animations:^{S1Button.frame = CGRectMake(20, 10, 50, 10);S1buttonA.alpha = 0;} 
        completion:nil];