如何使用自动布局动画的UITableViewCell高度? [英] How to animate UITableViewCell height using auto-layout?

查看:319
本文介绍了如何使用自动布局动画的UITableViewCell高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于的UITableViewCell 高度动画堆栈溢出类似的问题,但没有任何工程新iOS8上自动布局驱动表视图。我的问题:

There are lot of similar questions on Stack Overflow about UITableViewCell height animation, but nothing works for new iOS8 auto-layout driven table view. My issue:

自定义单元格:

@property (weak, nonatomic) IBOutlet iCarousel *carouselView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIButton *seeAllButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *carouselHeightConstraint;

请注意 carouselHeightConstraint 。这是内容视图的子视图高度约束(唯一的子视图)。

Note carouselHeightConstraint. This is height constraint for content view's subview (the only subview).

高度设置为 230.0f 为例。第一次加载它看起来像:

Height is set to 230.0f for example. On first load it looks like:

然后,在查看全部的行动我想的扩展细胞,我的code:

Then, on See All action I want to expand cell, my code:

- (IBAction)seeAllButtonAction:(id)sender {

  BOOL vertical = !self.carouselCell.carouselView.vertical;
  self.carouselCell.carouselView.type = vertical ? iCarouselTypeCylinder : iCarouselTypeLinear;
  self.carouselCell.carouselView.vertical = vertical;

  [UIView transitionWithView:self.carouselCell.carouselView
                    duration:0.2
                     options:0
                  animations:^{

    self.carouselCell.carouselHeightConstraint.constant = vertical ? CGRectGetHeight(self.tableView.frame) : 230;
    [self.carouselCell setNeedsUpdateConstraints];
    [self.carouselCell updateConstraintsIfNeeded];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

                  completion:^(BOOL finished) {
                  }];
}

正如你所看到的,我尝试使用这个历久弥新的解决方案:结果
<一href=\"http://stackoverflow.com/questions/460014/can-you-animate-a-height-change-on-a-uitableviewcell-when-selected\">Can选择当您在动画一个UITableViewCell的高度变化?

和结果:

我的手机的收缩 44.0f 高度。

为什么出现这种情况?我希望看到我的手机顺利,自动layot​​魔法扩大。

Why this happens? I expect to see my cell smoothly expanded with magic of auto-layot.

注意:结果
我dont't要使用 -tableView:heightForRowAtIndexPath:。它的自动布局时代,对吧?

Note:
I dont't want to use -tableView:heightForRowAtIndexPath:. It's auto-layout era, right?

推荐答案

下面为我工作:

preparation:


  1. viewDidLoad中告诉表视图使用自施胶细胞:

  1. On viewDidLoad tell the table view to use self-sizing cells:

tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44; // Some average height of your cells


  • 添加的约束,你通常会,但它们添加到该单元格的内容查看

    动画高度的变化:

    假如你改变了一个约束的

    Say you change a constraint's constant:

    myConstraint.constant = newValue;
    

    ...或者你添加/删除约束。

    ...or you add/remove constraints.

    要动画这一变化,步骤如下:

    To animate this change, proceed as follows:


    1. 告诉内容查看动画变化:

    1. Tell the contentView to animate the change:

    [UIView animateWithDuration: 0.3 animations: ^{ [cell.contentView layoutIfNeeded] }]; // Or self.contentView if you're doing this from your own cell subclass
    


  • 然后告诉表视图的高度变化反应与动画

  • Then tell the table view to react to the height change with an animation

    [tableView beginUpdates];
    [tableView endUpdates];
    


  • 0.3的第一个步骤的持续时间是什么似乎是主叫开始/ endUpdates时的UITableView用于其动画的持续时间。

    The duration of 0.3 on the first step is what seems to be the duration UITableView uses for its animations when calling begin/endUpdates.

    红利 - 变革的高度没有动画,没有重新加载整个表:

    如果你想要做同样的事情如上,但没有一个动画,然后做这个:

    If you want to do the same thing as above, but without an animation, then do this instead:

    [cell.contentView layoutIfNeeded];
    [UIView setAnimationsEnabled: FALSE];
    [tableView beginUpdates];
    [tableView endUpdates];
    [UIView setAnimationsEnabled: TRUE];
    

    摘要:

    // Height changing code here:
    // ...
    
    if (animate) {
        [UIView animateWithDuration: 0.3 animations: ^{ [cell.contentView layoutIfNeeded]; }];
        [tableView beginUpdates];
        [tableView endUpdates];
    }
    else {
        [cell.contentView layoutIfNeeded];
        [UIView setAnimationsEnabled: FALSE];
        [tableView beginUpdates];
        [tableView endUpdates];
        [UIView setAnimationsEnabled: TRUE];
    }
    


    您可以检查出我的实现,当用户选择它这里(纯斯威夫特和放大器,扩展单元的纯自动布局 - 真正的未来之路)。


    You can check out my implementation of a cell that expands when the user selects it here (pure Swift & pure autolayout - truly the way of the future).

    这篇关于如何使用自动布局动画的UITableViewCell高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆