【问题标题】:Expand and Collapse a UIView within a UIViewController在UIViewController中展开并折叠UIView
【发布时间】:2013-07-18 06:50:58
【问题描述】:

我想在单击按钮时展开视图并折叠它。并相应地更改位于视图下方的数据框架。我使用了滚动视图,然后添加了要滚动的项目。

我可以添加一个新视图并将其隐藏,然后单击 (+) 按钮我取消隐藏该视图,并隐藏 (+) 按钮。

两个问题

1) 如何管理视图下方的数据,以便当视图处于折叠位置时,数据应位于视图下方?当我展开它时,它应该相应地改变框架。
2)标签“标签是test1”是单个标签。我会在那里显示描述。最初是 4 行,当我们单击 (+) 按钮时,将显示剩余的描述。因此,如果我采用另一个视图,这是不可能的,因为我需要采用单个标签或文本视图来显示数据。

-(IBAction)plusButton:(id)sender{

    [plusButton setHidden:YES];
    [expandedView setHidden:NO];

    //[mainView setFrame:CGRectMake(10, 150, 294, 400)];

}

-(IBAction)minusButton:(id)sender{
    [plusButton setHidden:NO];
    [expandedView setHidden:YES];

}

【问题讨论】:

  • 这取决于,你是如何制作动画来关闭窗口的?我建议使用 [UIView animationWithDuration: animation: completion:] 块,因为您可以更有效地控制它,但请描述您的操作方式
  • @user2277872 : 请检查我编辑的问题
  • 我只是在隐藏和取消隐藏视图,主要担心的是,如果一个两个视图,我不能使用一个标签。
  • 你可以通过将它放在动画代码下设置视图框架,并且需要改变底部标签的y位置以及内容标签的框架高度。
  • @Herçules :但是我使用了两个视图,所以我如何在单个标签上显示数据并在两个视图上使用它。 ??

标签: ios ios5


【解决方案1】:

在我看来像是一个自动调整大小的问题。 您需要将标签和文本字段的自动调整大小设置为

UIViewAutoresizingFlexibleHeight 和 UIViewAutoresizingFlexibleTopMargin

我建议您将这些标签和文本字段放在具有相同自动调整大小属性的“内容”视图中,并将其添加为子视图。

编辑:(添加示例)

UIView *contentView = [[UIView alloc] initWithFrame:frame]; //contentView's initail frame.
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:contentView.bounds]; //in this example the label takes the whole frame of the contentView, you can change it to be smaller in height if it suits you purpose.

descriptionLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
contentView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;

点击“+”按钮:

[UIView animateWithDuration:0.2f animations:^{
    CGRect rect = contentView.frame;
    rect.size.height = 200;
    contentView.frame = rect;
}];

点击“-”按钮:

[UIView animateWithDuration:0.2f animations:^{
    CGRect rect = contentView.frame;
    rect.size.height = 100;
    contentView.frame = rect;
}];

【讨论】:

  • Lirik : 如果你给我一些显示上述例子的链接,那就太好了,这样我的概念就清楚了。
  • 在原始答案中添加了示例
  • Lirik : 朋友,我在 VDl 中使用了您上面的代码,其余的分别在 + 和 - 按钮中使用。但它没有反映任何变化。可能这与 Rezald 所做的相同,所以我必须告诉你我有一个滚动视图,然后将项目添加到其中。
【解决方案2】:

使用类似的动画

无论是 UIView 还是 UIlabel 都使用它们中的任何一个..

连接 containerView 插座以联系信息标签或视图。和底部视图出口标签包含“lorel ipsum ...”

//on plus button click

IBOutlet UILabel *containerView;
IBOutlet UILabel *bottomView;

CGRect rect=containerView.frame;
rect.size.height=400;

CGRect rect2=bottomView.frame;
rect2.origin.y=420;

[UIView animateWithDuration:0.3 animations:^{
   [containerView setFrame:rect];
    [bottomView setFrame:rect2];
}];


//on minus button click


CGRect rect=containerView.frame;
rect.size.height=300;

CGRect rect2=bottomView.frame;
rect2.origin.y=320;

[UIView animateWithDuration:0.3 animations:^{
    [containerView setFrame:rect];
    [bottomView setFrame:rect2];
}];

【讨论】:

  • Hercules :动画效果很好,但是当我点击屏幕上的任何位置时,它会到达原始位置。此外,我需要具有相同的标签,当视图未展开时显示一半数据以及视图展开时的持续剩余数据
  • 因为它在滚动视图中。单击滚动视图时,它会调用 didScroll 并返回到原始位置
  • 可能,但我们完全需要一个滚动视图以及标签的东西。
  • 标记或查看您使用的任何内容。我不会告诉您删除它。只是找出一些替代方案。
  • HerCules : 是的.. 但我的大脑现在还没有得到修复,请帮忙。也给你发一份演示副本。
【解决方案3】:

您可以使用这样的动画块在屏幕内外设置视图的动画,这是我的一个应用程序在视图内移动视图导致从底部向上滑动的示例。

  -(void) showFxAnimation {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.65];
     [self.sliderFxSubView setFrame:CGRectMake(0.0f, [UIScreen mainScreen].bounds.size.height - 150,320.0f , 150.0f)];
    [UIView commitAnimations];    
}


-(void) hideFxAnimation {

    [UIView animateWithDuration:1.0 animations:^{

        [self.sliderFxSubView setFrame:CGRectMake(0.0f, 1200.0f, 320.0f, 150.0f)];

    }];

}

【讨论】:

  • Rezand :sliderFxSubview 是要显示的第二个视图???如果是这样,那么我将上面的代码分别添加到(+)和(-)按钮,但它没有反映任何变化。
  • 你还在使用滚动视图吗?
  • Rezand :是的,我相信我们需要使用它,因为我们不知道标签将保存多少数据以及 View 的高度有多大。
  • 好的。仍然应该有一些效果,我想我现在自己在滚动视图中测试一下。
  • Rezand :恐怕没有。是的,如果您需要的话,那就太好了,我可以尽快给您发送一份演示副本,以便您快速进行更改。
猜你喜欢
  • 2018-11-02
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
相关资源
最近更新 更多