ios 如何在 iPhone 中隐藏/显示带有动画的 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18395633/
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-31 00:49:29  来源:igfitidea点击:

How to hide/show UIView with animation in iPhone

iosobjective-cuiviewuiviewanimation

提问by user2586519

I want to show UIView after button pressed with animation,I can show the view but I am unable to hide it again pressed that button.Here is my code to show/hide the view. To Show the UIView :

我想在按下带有动画的按钮后显示 UIView,我可以显示视图,但我无法再次隐藏它,按下该按钮。这是我显示/隐藏视图的代码。显示 UIView :

    sliderView.frame =  CGRectMake(130, 20, 0, 0);
    [UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 100, 200);
    }]; 

To Hide the UIView :

隐藏 UIView :

     [UIView animateWithDuration:0.25 animations:^{
     sliderView.frame =  CGRectMake(130, 30, 0, 0);
    }]; 

Using above code view is showing with animation but not hiding. Does anybody know how to hide it,Please help,Thanks

使用上面的代码视图显示动画但不隐藏。有没有人知道怎么隐藏它,请帮忙,谢谢

回答by MOBYTELAB

Your code works, i have used it. Look code below
.h file:

您的代码有效,我已经使用了它。查看
.h 文件下面的代码:

#import <UIKit/UIKit.h>

@interface StackoverflowTestViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *cautionView;


- (IBAction)btnToggleClick:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *btnToggle;
@end


.m file:


.m 文件:

#import "StackoverflowTestViewController.h"

@interface StackoverflowTestViewController ()

@end

@implementation StackoverflowTestViewController

bool isShown = false;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnToggleClick:(id)sender {
    if (!isShown) {
        _cautionView.frame =  CGRectMake(130, 20, 0, 0);
        [UIView animateWithDuration:0.25 animations:^{
            _cautionView.frame =  CGRectMake(130, 30, 100, 200);
        }];
        isShown = true;
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            _cautionView.frame =  CGRectMake(130, 30, 0, 0);
        }];
        isShown = false;
    }
}
@end

回答by Leena

  -(void)fadeInAnimation:(UIView *)aView {

    CATransition *transition = [CATransition animation];
    transition.type =kCATransitionFade;
    transition.duration = 0.5f;
    transition.delegate = self;
    [aView.layer addAnimation:transition forKey:nil];
    }

Add Subview:

     [self.view addsubView:mysubview]; 
     [self fadeInAnimation:self.view];

Remove SubView:

      [mySubView removefromSuperView];   
      [self fadeInAnimation:self.view];

回答by Muhammad Zeeshan

You can use alpha property and hidden property of UIView to hide your sliderView. Try the following code:

您可以使用 UIView 的 alpha 属性和 hidden 属性来隐藏您的滑块视图。试试下面的代码:

/*To unhide*/
[sliderView setHidden:NO];
sliderView.frame =  CGRectMake(130, 20, 0, 0);
[UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 100, 200);
    sliderView.alpha = 1.0f;
} completion:^(BOOL finished) {
}];

/*To hide*/
[UIView animateWithDuration:0.25 animations:^{
    sliderView.frame =  CGRectMake(130, 30, 0, 0);
    [sliderView setAlpha:0.0f];
} completion:^(BOOL finished) {
    [sliderView setHidden:YES];
}];

回答by iosdeveloper

try this

尝试这个

- (IBAction)eventparameter:(id)sender
{
    if ([self.parameterview isHidden])
    {
        [UIView beginAnimations:@"LeftFlip" context:nil];
        [UIView setAnimationDuration:0.8];
        _parameterview.hidden=NO;
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown    forView:_parameterview cache:YES];
        [UIView commitAnimations];   
    }
    else
    {
        [UIView transitionWithView:_parameterview
                          duration:0.4
                           options:UIViewAnimationOptionTransitionCurlUp
                        animations:NULL
                        completion:NULL];

        [self.parameterview  setHidden:YES]; 
    }
}

回答by iosdeveloper

Apply Initial/starting frame of you View is

应用您视图的初始/起始帧是

sliderView.frame =  CGRectMake(130, 480, 100, 200);

Give tag of your button such like

给你的按钮标签,比如

myButton.tag = 101;

myButton.tag = 101;

And your button method

还有你的按钮方法

-(void)MyButonMethod:(UIButton *)sender
{
  if(sender.tag == 101)
  {
    [UIView animateWithDuration:0.25 animations:^{
         sliderView.frame =  CGRectMake(130, 30, 100, 200);
    }]; 

    sender.tag = 102;
  }
  else
  {
    [UIView animateWithDuration:0.25 animations:^{
         sliderView.frame =  CGRectMake(130, 480, 100, 200);
    }];     
    sender.tag = 101;  
  }
}

回答by Hristo

Try this:

尝试这个:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.25];
[sliderView setFrame:CGRectMake(130, 30, 0, 0)];
[UIView commitAnimations];