iPhone如何通过动画隐藏/显示UIView

7

我希望在按钮按下后显示UIView并附带动画效果,我能够显示视图,但是我无法再次按下该按钮来隐藏它。以下是我用于显示/隐藏视图的代码。 显示UIView的代码:

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

隐藏UIView的方法如下:
     [UIView animateWithDuration:0.25 animations:^{
     sliderView.frame =  CGRectMake(130, 30, 0, 0);
    }]; 

使用上述代码,视图会以动画方式显示,但不会隐藏。有人知道如何隐藏它,请帮忙,谢谢。


你发布的代码应该将 sliderView 缩小到零。但实际上会发生什么?你没有告诉我们实际问题是什么。 - rmaddy
那段代码怎么能显示视图呢?它将框架的宽度和高度缩小为0。你确定调用了隐藏视图的代码吗? - rmaddy
你是在什么地方调用这些方法?是一个接一个地调用还是通过某些事件触发调用的?sliderView 在这两个方法中是同一个对象吗? - Jsdodgers
检查一下你是否调用了隐藏操作。 - NANNAV
@rmaddy和NANNAV,是的,隐藏视图的代码正在被调用,因为我正在使用标志进行检查。 - user2586519
显示剩余2条评论
6个回答

18

您的代码有效,我已经使用过它。请查看下面的代码
.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文件:

#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

谢谢Binh Lee,我还想在那个UIView上添加ImageView,所以我能像UIView一样改变ImageView的框架吗? - user2586519
没问题,(1)你可以将ImageView添加到UIView上,并在该UIView上更改框架(2)毫无问题地用ImageView更改上面的UIView - MOBYTELAB
@BinhLee 我正在使用你的代码,其中_cautionView是有动画效果的。我已经在_cautionView中添加了两个UILabel。这些Label在动画过程中不会隐藏。 - Thangavel

6
  -(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];

4
你可以使用UIView的alpha属性和hidden属性来隐藏你的sliderView。尝试以下代码:
/*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];
}];

这对我有效.. 如果您正在使用故事板,可以添加一个UI视图并将其大小设置为所需大小。如果是这样,您可以删除sliderView.frame的CGRectMake值。 - Udaya Sri

2

试一试

- (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]; 
    }
}

0

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

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

给你的按钮打上标签,例如

myButton.tag = 101;

以及你的按钮方法

-(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;  
  }
}

0

试试这个:

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

2
这些已经过时了。苹果建议您使用块版本(如原问题所示)。 - rmaddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接