【问题标题】:Animate UILabel in UITableViewCell在 UITableViewCell 中为 UILabel 设置动画
【发布时间】:2013-08-29 15:42:38
【问题描述】:

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

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

但这似乎什么也没做。我认为这是关于约束,但我不知道如何处理,我正在使用自动布局。

感谢您的帮助

【问题讨论】:

    标签: ios objective-c uitableview cocoa-touch uikit


    【解决方案1】:

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

    【讨论】:

    • 确实如此。最后一个不需要框架操作的答案,在处理自动布局时并不是很强大。
    • 非常好的答案谢谢,我考虑过约束,但不知道我能够设置一个约束的出口,现在我学会了:)
    • @user1396236,我已经添加到我的答案中,以展示我是如何实现的,包括如果您再次单击单元格,则取消选中它们。
    【解决方案2】:

    您正在尝试调整单元格的框架,但没有任何效果。尝试调整单元格的contentView 的框架。

    【讨论】:

      【解决方案3】:

      我想下面的功能会解决你的问题 将您的标签作为 cell.textLabel.layer 传递,然后像

      一样传递点
      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"];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多