Search code examples
iphoneobjective-cuitableviewcore-animation

UITableViewCell Animation Question


i have a UITableViewCell, and i am trying to add an animation that causes a layer to scroll off of the cell, the problem is it gets clipped by the cell above it, is there an easy way to get around this?

here is my method.

- (void)animateLife:(UIButton *)sender isPositive:(BOOL)state{

    // Reset lCount if we are pressing a different button than last time.
    if (sent != sender) {
        lCount = 0;
    }
    sent = sender;

    // Increase the count by 1.
    lCount = lCount + 1;

    // Set up some properties.
    CGPoint origin = sender.center;
    CGPoint newOrigin = CGPointMake(origin.x, (origin.y - 100));
    NSString *oper = [[NSString alloc] init];

    // Check to see if this is a + or - change.
    if (state == true) {
        oper = @"+";
    } else {
        oper = @"-";
    }

    // Alloc a String and a Label to hold it.
    NSString *characters = [[NSString alloc] initWithFormat:@"%@%d", oper, lCount];
    UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(origin.x,origin.y , 50, 50)];

    // Configure the Label.
    if (oper == @"-") {
        [theLabel setTextColor:[UIColor redColor]];
    } else {
        [theLabel setTextColor:[UIColor greenColor]];  
    }
    [theLabel setText:characters];
    [theLabel setBackgroundColor:[UIColor clearColor]];
    [theLabel setAlpha:1];
    [theLabel setCenter:origin];
    [theLabel setShadowColor:[UIColor blackColor]];
    [theLabel setShadowOffset:CGSizeMake(0.5, 0.5)];
    [theLabel setFont:[UIFont fontWithName:@"Helvetica" size:28]];
    [theLabel setTextAlignment:UITextAlignmentCenter];
    [theLabel.layer setShadowRadius:3];
    [theLabel.layer setShadowOpacity:0.9];

    // Display our Label.
    [sender.superview insertSubview:theLabel aboveSubview:sender];

    // Setup animation.
    [UIView beginAnimations:@"lifeCount" context:nil];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [theLabel setCenter:newOrigin];
    [theLabel setAlpha:0];

    // Commit Animation.
    [UIView commitAnimations];

    fCount = fCount + 1;
    // Clean up.
    [theLabel release];
    [characters release];
    [oper release];
}

- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {

    // Since an animation finished, reduce count by one.
    fCount = fCount - 1;

    //check if all animations are finished, if so, reset the lCount.
    if (fCount < 1) {
        lCount = 0;
    }
}

Update: it appears if i scroll cells to where they get reused, then the animation shows up properly is there a way i can make it not require the reuse?


Solution

  • In your method cellForRowAtIndexPath there is probably code like this:

     cell = [tableView dequeueReusableCellWithIdentifier:someCellID];
     if ( cell == nil ) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:kCellID] autorelease];
     }
    

    Do not call the "dequeueReusableCellWithIdentifier" method. Just create new cell each time.

    Note that if you have a lot of rows in your table, this will have a performance hit.