2013-08-29 145 views
1

我有一個自定義的UITableViewCell,帶有三個標籤 - 標題,副標題和右側細節。 現在,當用戶點擊單元格時,這個單元格會被正常的複選標記檢查,但我需要爲左側的正確標籤設置動畫。我以爲我可以做在UITableViewCell中動畫UILabel

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

但這似乎什麼都不做。我認爲這是關於約束,但我不知道如何處理這個問題,我使用自動佈局。

感謝您的任何幫助

回答

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]; 
     }]; 
    } 
} 
+0

確實。最後一個答案不需要幀處理,這在處理自動佈局時不是很健壯。 – Abizern

+0

非常好的答案謝謝,我想約束,但不知道我能夠建立一個出口約束,現在我學會了:) – user1396236

+0

@ user1396236,我已經添加到我的答案,顯示我如何做包括取消檢查單元格,如果你再次點擊它們。 – rdelmar

0

您試圖調整單元格的框架,將不會有任何效果。嘗試調整單元的框架contentView

0

我想下面的功能將解決您的問題 通過你的標籤爲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"]; 
}