gpt4 book ai didi

ios - 在 UITableViewCell 中为 UILabel 设置动画

转载 作者:行者123 更新时间:2023-12-01 19:08:11 25 4
gpt4 key购买 nike

我有一个带有三个标签的自定义 UITableViewCell - 标题、副标题和正确的细节。
现在,当用户点击单元格时,此单元格将使用正常的复选标记进行检查,但我需要将右侧标签设置为动画左侧。我以为我能做到

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

但这似乎根本没有做任何事情。我认为这是关于约束,但我不知道如何处理,我正在使用自动布局。

谢谢你的帮助

最佳答案

您的 playerRatingLabel 应该对单元格的右边缘有一个约束。您的自定义单元格需要为该约束创建一个 IBOutlet。然后在单元格点击上,为该约束的常量参数设置动画(我在示例中将 socket 称为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com