UITableViewCell动画问题

时间:2011-07-09 13:37:25

标签: iphone objective-c uitableview core-animation

我有一个UITableViewCell,我试图添加一个动画,导致一个图层滚动离开单元格,问题是它被它上面的单元格修剪,是否有一种简单的方法可以解决这个问题? / p>

这是我的方法。

- (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;
    }
}

更新:如果我将单元格滚动到可以重复使用的位置,则会出现,然后动画正确显示有没有办法可以让它不需要重复使用?

1 个答案:

答案 0 :(得分:0)

在您的方法cellForRowAtIndexPath中,可能存在以下代码:

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

不要调用“dequeueReusableCellWithIdentifier”方法。只需每次创建新单元格。

请注意,如果表格中有很多行,则会产生性能影响。