我正在尝试一个简单的动画来选择/取消选择UITableViewCell,如下所示:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[UIView animateWithDuration: 0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
tabConstraint.constant = selected ? 40 : 20;
} completion:nil];
}
动画块内的代码将被调用,但它不是动画。一切都很好,但根本没有任何动画。我怎样才能让细胞选择动画?
答案 0 :(得分:2)
每次更新autolayout约束时,都必须调用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];
}
答案 1 :(得分:2)
您需要在layoutIfNeeded
区块中致电animations
。有关详细信息,请查看此问题的已接受答案:How do I animate constraint changes?
答案 2 :(得分: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];
}