Iphone 通过使父视图的高度为0来隐藏包含的所有视图

Iphone 通过使父视图的高度为0来隐藏包含的所有视图,iphone,ios,xcode,ios4,ios6,Iphone,Ios,Xcode,Ios4,Ios6,我正在制作一个动画,其中一个名为“子视图””的UIView的动画的高度必须为0。 现在,该子视图包含一个UIButton和UIImageView。 现在,我在单击按钮时使用此方法使高度为0 @interface ViewController : UIViewController { IBOutlet UIView *suvView; IBOutlet UIButton *subButton; IBOutlet UIImageView *imgview; } -(IBAct

我正在制作一个动画,其中一个名为“子视图””的
UIView
的动画的高度必须为0。 现在,该子视图包含一个
UIButton
UIImageView
。 现在,我在单击按钮时使用此方法使高度为0

@interface ViewController : UIViewController
{
    IBOutlet UIView *suvView;
    IBOutlet UIButton *subButton;
    IBOutlet UIImageView *imgview;
}
-(IBAction)hide:(id)sender;
@end


@implementation ViewController

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

-(IBAction)hide:(id)sender
{
    [UIView beginAnimations:nil context:suvView];
    [UIView setAnimationDuration:0.25];
    [suvView setFrame:CGRectMake(0, 94, 320, 0)];
    [UIView commitAnimations];
}
@end
但当我将高度设为0时,名为“subButton”和“imgview”的视图看起来就是这样。 我想在子视图高度为0时将其隐藏


我使用的是XCode 4.5,您有两种方法:

首先,如果要使SUV视图高度为0,则无需将其删除。但只需隐藏imgview和子按钮即可

-(IBAction)hide:(id)sender
{
    [UIView beginAnimations:nil context:suvView];
    [UIView setAnimationDuration:0.25];
    [suvView setFrame:CGRectMake(0, 94, 320, 0)];
    [UIView commitAnimations];
    subButton.hidden = YES;
    imgview.hidden = YES;
}
@end
您还可以使用suvView的高度来降低subButton和imgview的高度,如

-(IBAction)hide:(id)sender
{
    [UIView beginAnimations:nil context:suvView];
    [UIView setAnimationDuration:0.25];
    [suvView setFrame:CGRectMake(0, 94, 320, 0)];
    [imgview setFrame:CGRectMake(0, x, y, z];
    [subButton setFrame:CGRectMake(0, x, y, z];
    [UIView commitAnimations];
}

其中x、y、z是imgview和子按钮的坐标。希望这有帮助。

在开始动画之前添加以下行:

subButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
或者,您也可以在界面生成器中通过为每个子视图(按钮和图像视图)设置垂直和水平调整大小来完成此操作


顺便说一句,您应该使用苹果推荐的基于块的动画(如果您使用的是iOS 4.0及以上版本),而不是
beginAnimations:
committeanimations:

如果要在不调整大小的情况下隐藏它们:
[mainview setclipstobunds:YES]

Jonathans回答是最简单的方法。
//try this code for view animation

-(IBAction)hide:(id)sender
{
[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationCurveEaseOut
                 animations:^{
    [suvView setFrame:CGRectMake(0, 94, 320, 0)];
 }
  completion:nil];
}