[英]Animation to Hide UIButton in iOS
In my application i want to give an animation to UIButtons
, like the buttons will "fall out" of the screen when they hide.在我的应用程序中,我想给
UIButtons
一个动画,就像按钮在隐藏时会“掉出”屏幕一样。
I tried the following code but it didn't give me a good result.我尝试了以下代码,但没有给我一个好的结果。
[UIView animateWithDuration:1.5
animations:^{
S1Button.frame = CGRectMake(20, 10, 50, 10);
}];
[S1Button setHidden:YES];
break;
You can set new position and hide the button after animation.您可以设置新位置并在动画后隐藏按钮。
[UIView animateWithDuration:0.9 animations:^{
tradeButton.frame = (CGRect){ CGPointMake(51, 150), tradeButton.bounds.size };
} completion:^(BOOL finished) {
tradeButton.hidden = YES;
// etc.
}];
Use the animation method which has a completion block and hide the button there.使用具有完成块的动画方法并将按钮隐藏在那里。 Currently your hide method runs immediately so you don't see the animation.
目前您的 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];
}];
Try this one试试这个
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;
}];
In your Code, The hidden property of Button is NOT animatable.在您的代码中, Button 的 hidden 属性不可设置动画。 When this animation block runs, your button will immediately hidden , but it will not fade/animate.
当此动画块运行时,您的按钮将立即隐藏,但不会淡入淡出/设置动画。 The appropriate way to fade out a UIView is to animate its alpha property from 1.0 to 0.0 like this:
淡出 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];
try this:尝试这个:
you hide the button before your animation completion, the no animation is visible.在动画完成之前隐藏按钮,则没有动画可见。 So, replace your code with this:
所以,用这个替换你的代码:
[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.