2

我正在制作一个动画,在其中我必须将一个UIView名为“ subView ”的高度设为 0。现在 subView 包含一个UIButtonUIImageView。现在我通过这种方法在按钮单击时将高度设为 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

4

4 回答 4

6

如果您想隐藏它们而不调整大小:[mainview setClipsToBounds:YES]

于 2012-10-15T09:30:45.623 回答
0

你有两种方法可以做到这一点: -

首先,如果您将 suvViews 高度设为 0,则无需将其移除。但只需隐藏 imgview 和 subButton 之类的

-(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 的坐标。希望这可以帮助。

于 2012-10-15T09:17:46.163 回答
0

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

subButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

或者您也可以从界面构建器中执行此操作,方法是为每个子视图(按钮和图像视图)设置垂直和水平调整大小

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

于 2012-10-15T09:20:16.390 回答
0
//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];
}
于 2012-10-15T09:21:15.117 回答