1

I have a custom UITableViewCell with three labels - title, subtitle and right detail. Now when a User taps on Cell this Cell becomes checked with the normal Checkmark, but I need to animate the right label to the left. I thought I could just do

CGRect rect = cell.playerRatingLabel.frame;
rect.origin.x -= 10;
[CLCPlayerViewCell animateWithDuration:1.0 animations:^{
    cell.playerRatingLabel.frame = rect;
}];

but this appears to do nothing at all. I think it's about constraints but I don't know how to handle this, I'm using auto Layout.

Thanks for any help

4

3 回答 3

3

您的 playerRatingLabel 应该对单元格的右边缘有一个约束。您的自定义单元格需要为该约束创建一个 IBOutlet。然后在单元格点击上,为该约束的常量参数设置动画(我在示例中将插座称为 rightCon):

[UIView animateWithDuration:1.0 animations:^{
    cell.rightCon.constant = 30; // change this value to meet your needs
    [cell layoutIfNeeded];
}];

这是我用来执行此操作的完整实现。我的自定义单元格有两个标签,当您单击一个单元格并添加复选标记时,我会为右侧的标签设置动画。我创建了一个属性 selectedPaths (一个可变数组)来跟踪检查的单元格。如果单击已选中的单元格,它将取消选中它,并将标签动画返回到其原始位置。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.leftLabel.text = self.theData[indexPath.row];
    cell.accessoryType = ([self.selectedPaths containsObject:indexPath])? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
    cell.rightCon.constant = ([self.selectedPaths containsObject:indexPath])? 40 : 8;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RDCell *cell = (RDCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (! [self.selectedPaths containsObject:indexPath]) {
        [self.selectedPaths addObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [UIView animateWithDuration:.3 animations:^{
            cell.rightCon.constant = 40;
            [cell layoutIfNeeded];
        }];
    }else{
        [self.selectedPaths removeObject:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
        [UIView animateWithDuration:.3 animations:^{
            cell.rightCon.constant = 8;
            [cell layoutIfNeeded];
        }];
    }
}
于 2013-08-29T15:55:53.523 回答
0

我想下面的函数将解决你的问题将你的标签传递为 cell.textLabel.layer 和 den 传递点像

CGPointMake(0, self.view.center.y)

-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
// Prepare the animation from the current position to the new position
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [layer valueForKey:@"position"];
animation.duration = 0.2;

animation.toValue = [NSValue valueWithCGPoint:point];

// Update the layer's position so that the layer doesn't snap back when the animation completes.
layer.position = point;

// Add the animation, overriding the implicit animation.
[layer addAnimation:animation forKey:@"position"];
}
于 2013-08-29T15:58:16.527 回答
0

您正在尝试调整不会产生任何影响的单元格的框架。尝试调整单元格的框架contentView

于 2013-08-29T15:54:47.800 回答