简体   繁体   English

UIView animateWithDuration UITableView也会导致UITableViewCell子视图动画

[英]UIView animateWithDuration UITableView cause UITableViewCell subview animates too

I am trying to animate change UITableView height constraint using + transitionWithView:duration:options:animations:completion: . 我正在尝试使用+ transitionWithView:duration:options:animations:completion:动画更改UITableView高度约束+ transitionWithView:duration:options:animations:completion: . When the options is set, the UITableViewCell contentView's subview UIView also animates. 设置选项后,UITableViewCell contentView的子视图UIView也会动画显示。 The effect is like below. 效果如下。 As you can see the cell underneath the window goes up and the red dot view's bounds animates to the predefined constraint. 正如您所看到的那样,窗口下方的单元格向上,红点视图的边界将动画化为预定义的约束。 If I don't set the animation options, it won't be like this. 如果我没有设置动画选项,它将不会是这样的。 But I need the animation when I change the UITableView height. 但是当我改变UITableView高度时我需要动画。 So how to keep the table view height animation and disable the table view cell contentView's sub view animation? 那么如何保持表视图高度动画并禁用表视图单元格contentView的子视图动画? The code is below. 代码如下。 The red dot view is a UIView with a red background color. 红点视图是具有红色背景颜色的UIView。 Is there any way to disable UITableViewCell contentView's subview animation? 有没有办法禁用UITableViewCell contentView的子视图动画?

- (void)changeTableViewHeight {
    self.tableViewTopVerticalSpaceConstraint.constant = 0;
    [self.tableView setNeedsUpdateConstraints];
    [self.view setNeedsUpdateConstraints];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        self.topViewHeightConstraint.constant = 50;
        [self.topView setNeedsUpdateConstraints];
    }];
}

动画

//1. All constraints change must be done
self.hightConstraint.constant = 200;
self.anotherConstraint.constant = 34;
...
...

// 2. Indimate to iOS that this view need update constraints. 
// Hey iOS I have modified few constraints. beware of it to layout that changes.
[self.view setNeedsUpdateConstraints];


// 3. I'm done, relayout the view with animation, 
      If I really changed any constraints  to view.
[UIView animateWithDuration:1.5 animations:^{
    [self.view layoutIfNeeded];
}];

Hope somebody will save their time 希望有人能节省时间

Had a similar problem that I solved with disabling animation for the layer of the view I didn't want animated. 有一个类似的问题,我解决了禁用动画的视图层我不想动画。 In your case ... implement the awakeFromNib method for your UITableViewCell subclass and disable the animations there. 在你的情况下...为你的UITableViewCell子类实现awakeFromNib方法并在那里禁用动画。 In your case something like ... 在你的情况下像......

override func awakeFromNib() {
    super.awakeFromNib()
    redCircleView.layer.removeAllAnimations()
}

The thing is that UIView.animateWithDuration animates the views' layers and for new table view cells and other views created during or because of this animation, all changes will also be animated. 问题在于UIView.animateWithDuration为视图的图层UIView.animateWithDuration动画,对于在此动画期间或由此动画创建的新表视图单元格和其他视图,所有更改也将进行动画处理。

... EDIT ... ......编辑......

In the meantime I managed to find an even better solution. 与此同时,我设法找到了更好的解决方案。 In the options parameter for the UIView.animateWithDuration use the UIViewAnimationOptionLayoutSubviews (or .LayoutSubviews in Swift) option. UIView.animateWithDuration的options参数中,使用UIViewAnimationOptionLayoutSubviews (或Swift中的.LayoutSubviews )选项。 This layouts the subviews at animation commit time. 这将在动画提交时布置子视图。

update code 更新代码

- (void)changeTableViewHeight {
    self.tableViewTopVerticalSpaceConstraint.constant = 0;
    self.topViewHeightConstraint.constant = 50;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
       [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
       [self.topView setNeedsUpdateConstraints];
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM