[英]Animation to Hide UIButton in iOS
在我的應用程序中,我想給UIButtons
一個動畫,就像按鈕在隱藏時會“掉出”屏幕一樣。
我嘗試了以下代碼,但沒有給我一個好的結果。
[UIView animateWithDuration:1.5
animations:^{
S1Button.frame = CGRectMake(20, 10, 50, 10);
}];
[S1Button setHidden:YES];
break;
您可以設置新位置並在動畫后隱藏按鈕。
[UIView animateWithDuration:0.9 animations:^{
tradeButton.frame = (CGRect){ CGPointMake(51, 150), tradeButton.bounds.size };
} completion:^(BOOL finished) {
tradeButton.hidden = YES;
// etc.
}];
使用具有完成塊的動畫方法並將按鈕隱藏在那里。 目前您的 hide 方法會立即運行,因此您看不到動畫。
[UIView animateWithDuration:1 animations:^{
S1Button.frame = CGRectMake(20, 10, 50, 10);
} completion:^(BOOL finished) {
[S1Button setHidden:YES];
}]
[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];
}];
試試這個
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;
}];
在您的代碼中, Button 的 hidden 屬性不可設置動畫。 當此動畫塊運行時,您的按鈕將立即隱藏,但不會淡入淡出/設置動畫。 淡出 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];
嘗試這個:
在動畫完成之前隱藏按鈕,則沒有動畫可見。 所以,用這個替換你的代碼:
[UIView animateWithDuration:1.5 animations:^{
S1Button.frame = CGRectMake(20, 10, 50, 10);
} completion:^(BOOL finished) {
[S1Button setHidden:YES];
}];
break;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.