簡體   English   中英

在UITableViewCell中動畫UILabel

[英]Animate UILabel in UITableViewCell

我有一個帶有三個標簽的自定義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];
        }];
    }
}

您正在嘗試調整單元格的框架,這將不會產生任何影響。 嘗試調整單元格contentView的框架。

我想下面的函數將解決你的問題通過你的標簽作為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"];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM