1

我正在尝试一个简单的动画来选择/取消选择 UITableViewCell ,如下所示:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        tabConstraint.constant = selected ? 40 : 20;

    } completion:nil];
}

动画块内的代码将被调用,但它不是动画。一切正常,但根本没有任何动画。我怎样才能使单元格选择动画?

4

3 回答 3

2

每次更新自动布局约束时,都必须调用 layoutIfNeeded,

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {


    [UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{

        tabConstraint.constant = selected ? 40 : 20;
        [self layoutIfNeeded];

    } completion:nil];
}
于 2015-05-07T09:56:50.553 回答
2

你需要打电话给layoutIfNeeded你的animations街区。查看有关此问题的已接受答案以获取更多详细信息:如何为约束更改设置动画?

于 2015-05-07T09:57:22.050 回答
0

您需要layoutIfNeeded()在动画块内调用:-)

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    tabConstraint.constant = selected ? 40 : 20;
    [UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        whateverTabYouHaveHere.layoutIfNeeded()
    } completion:nil];
}
于 2015-05-07T09:58:48.283 回答