我有一个带有三个标签的自定义UITableViewCell-标题,副标题和正确的细节。
现在,当用户点击“单元格”时,该单元格将使用普通的选中标记进行选中,但是我需要为左侧的右侧标签设置动画。我以为我可以

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

但这似乎无济于事。我认为这与约束有关,但我不知道如何处理,我正在使用自动布局。

谢谢你的帮助

最佳答案

您的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];
        }];
    }
}

关于ios - 在UITableViewCell中对UILabel进行动画处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18515391/

10-12 00:08
查看更多