1

I'm trying a simple animation for selection/deselection of a UITableViewCell like this:

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

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

        tabConstraint.constant = selected ? 40 : 20;

    } completion:nil];
}

The code inside the animations block will be called, but it's not animating. Everything works fine, but there's not any animation at all. How could I make the cell selection animated?

Display Name
  • 4,502
  • 2
  • 47
  • 63

3 Answers3

2

Every time you update a autolayout constraint, you have to call 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];
}
Display Name
  • 4,502
  • 2
  • 47
  • 63
Gokul G
  • 698
  • 4
  • 11
2

You need to call layoutIfNeeded in your animations block. Check out the accepted answer on this question for more details: How do I animate constraint changes?

Community
  • 1
  • 1
bgilham
  • 5,909
  • 1
  • 24
  • 39
0

You need to call layoutIfNeeded() inside the animation block :-)

- (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];
}
Schemetrical
  • 5,506
  • 2
  • 26
  • 43