Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

animate textlabel in uitableviewcell using willTransitionToState

Tags:

iphone

I am trying to animate the textlabel in a UItableviewcell when I press the edit button. I am trying to make it fade out and fade in. fading in works but when I press 'edit' the textlabel disappears and when I press on 'done' I fades in just perfectly.

Can anyone tell me why it isn't working?

thanks in advance

- (void)willTransitionToState:(UITableViewCellStateMask)state {
 [super willTransitionToState:state];

 if ((state & UITableViewCellStateEditingMask) || (state & UITableViewCellStateShowingDeleteConfirmationMask)) {
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.3];
  label.alpha = 0.0;
  [UIView commitAnimations];
 }
}

- (void)didTransitionToState:(UITableViewCellStateMask)state {
 [super didTransitionToState:state];

 if (!(state & UITableViewCellStateEditingMask) && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.5];
  label.alpha = 1.0;
  [UIView commitAnimations];
 }
}
like image 652
Nico Avatar asked Nov 23 '10 11:11

Nico


2 Answers

I noticed that when entering willTransitionToState that animations were disabled. The following fixed it.

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];

    //Should be enabled by default...but apparently not
    [UIView setAnimationsEnabled:YES];
...
}
like image 150
Adam S. Avatar answered Nov 09 '22 13:11

Adam S.


From everything I had read I thought for sure the willTransitionToState was the way to go. It even works perfectly if you use didTransitionToState though the transition starts after the normal editing transition finishes.

As it turns out I think you want to use setEditing

- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
    [super setEditing:editing animated:animate];

    if(editing) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        label.alpha = 0.0;
        [UIView commitAnimations];
    } else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        label.alpha = 1.0;
        [UIView commitAnimations];
    }
}
like image 34
user579091 Avatar answered Nov 09 '22 13:11

user579091