如何在 iOS 中显示/隐藏带有动画的 UIView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8542506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:55:25  来源:igfitidea点击:

How to show/hide a UIView with animation in iOS?

iphoneiosipaduiviewuiviewanimationtransition

提问by user403015

The main UIView contains two subviews - UIView_1and UIView_2.
In the UIView_2, there is a button to show or hide the UIView_1.
For example, when a user touches the button to show the UIView_1, then UIView_1will slide down and UIView_2will push downwards with transition.
I have very little knowledge in animation. Can someone show me some sample code for reference?
Should I use CGAffineTransformMakeTranslation?
Thanks.enter image description here

主 UIView 包含两个子视图 -UIView_1UIView_2.
在 中UIView_2,有一个按钮可以显示或隐藏UIView_1
例如,当用户触摸按钮以显示UIView_1,然后UIView_1将向下滑动并UIView_2随着过渡向下推
我对动画知之甚少。有人可以给我看一些示例代码以供参考吗?
我应该使用 CGAffineTransformMakeTranslation 吗?
谢谢。在此处输入图片说明

回答by Shaggy Frog

You don't need anything so complex. Just change the view's frame size.

你不需要这么复杂的东西。只需更改视图的框架大小。

    NSTimeInterval animationDuration = /* determine length of animation */;
    CGRect newFrameSize = /* determine what the frame size should be */;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    theViewToChange.frame = newFrameSize;
    [UIView commitAnimations];

回答by Darshit Shah

Simply Hide/show with fadein/out effect `

只需使用淡入/淡出效果隐藏/显示`

/To Show/

/显示/

sliderView.hidden = NO;
sliderView.alpha = 0.1;
[UIView animateWithDuration:0.25 animations:^{
    sliderView.alpha = 1.0f;
} completion:^(BOOL finished) {
    // do some
}];

/To hide/

/隐藏/

[UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 0, 0);
    [sliderView setAlpha:0.1f];
} completion:^(BOOL finished) {
    sliderView.hidden = YES;
}];

`

`

回答by RyanM

It depends on what you want to do with UIView_2.

这取决于你想做什么UIView_2

  1. Place UIView_1below UIView_2in Interface Builder.

  2. Size UIView_2to take up all the space below the UINavigationBar.

  3. Use the following code to either resize (using uiview2_resized_rect) the frame for UIView_2, or translate/move the frame for UIView_2(using uiview2_translated_rect):

  1. UIView_1下面的UIView_2界面生成器。

  2. 大小UIView_2以占用UINavigationBar.

  3. 使用以下代码调整(使用uiview2_resized_rect)框架的大小UIView_2,或平移/移动框架UIView_2(使用uiview2_translated_rect):


CGRect uiview1_original_rect = UIView_1.frame;
CGRect uiview2_original_rect = UIView_2.frame;

CGRect uiview2_translated_rect = CGRectMake(uiview2_original_rect.origin.x, uiview2_original_rect.origin.y+uiview1_original_rect.size.height, uiview2_original_rect.size.width, uiview2_original_rect.size.height);

CGRect uiview2_resized_rect = CGRectMake(uiview2_original_rect.origin.x, uiview2_original_rect.origin.y+uiview1_original_rect.size.height, uiview2_original_rect.size.width, uiview2_original_rect.size.height-uiview1_original_rect.size.height);

[UIView animateWithDuration:0.300 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionBeginFromCurrentState animations:^{ //uncomment this and comment out the other if you want to move UIView_2 down to show UIView_1 //UIView_2.frame = uiview2_translated_rect; UIView_2.frame = uiview2_resized_rect; } completion:^(BOOL finished) {

}];