[英]Animate UILabel text change inside UITableViewCell
當文本屬性更改時,我想為自定義UITableViewCell
的UILabel
設置動畫。
我的動畫效果很好,除了動畫在每次滾動和調用[myTableView reloadData]
時每次進入單元格時都會觸發。
使用簡單的東西
if (![myLabel.text isEqualToString:newText]) {
// change myLabel.text with animation here.
}
不起作用。
我了解為什么會這樣。 如果我每次都創建一個新單元格,則可以檢查myLabel.text
是否包含字符串,如果有,是否不同,然后應用更改。 但是,有時會有太多的單元格無法顯示。
當重用單元格時, myLabel.text
幾乎總是已經有一個字符串,並且幾乎總是與我要設置的字符串不同。 因此上述檢查無效。 無論如何,它們將導致動畫觸發。
我想讓它起作用的唯一方法是使用myLabel.text
的最后一個已知值創建一個數組並進行比較,但是此列表非常龐大!
我不知道從哪里開始解決這個問題。
我想要的是myLabel
在text
屬性更改時在我的子類UITableViewCell
做一些flip / backgroundColor動畫。 我找到的最接近的解決方案將導致tableviewcell中的所有標簽運行動畫。
在給我一個字符串之前,我也無法檢查myLabel.text
,因為它總是被重用。
我開始覺得這是做不到的。
如果已將UITableViewCell
子類化,則可以覆蓋默認的prepareForReuse
方法。 在此方法中,將標簽的text屬性設置為nil
。
- (void)prepareForReuse {
[super prepareForReuse];
myLabel.text = nil;
}
現在:
if (myLabel.text && ![myLabel.text isEqualToString:newText]) {
// change myLabel.text with animation here.
}
現在,僅當將文本從一個文本更改為新文本時,該文本才應設置動畫。
prepareForReuse
dequeueReusableCell
的dequeueReusableCell
方法在單元格上調用prepareForReuse
。
#pragma mark - Timer Function
-(void)callAfteroneSecond_deals
{
NSArray *indexes = [self.tbl_deals indexPathsForVisibleRows];
MYTableViewCell *cell = (MYTableViewCell *)[_tbl_deals cellForRowAtIndexPath:index];
cell.lbl_expiredtime.text=@"Hello";
[cell.contentView bringSubviewToFront:cell.lbl_expiredtime];
}
我將在自定義單元格上定義一個方法,如下所示:
- (void) updateMyLabel: (NSString *)newText {
// If the new value is different than what is already set,
// and the label isn't nil, then trigger the animation
if (self.myLabel.text && self.myLabel.text != newText) {
[UIView animateWithDuration...];
}
self.myLabel.text = newText;
}
然后在tableView的cellForRowAtIndexPath中調用該方法。 棘手的是,隨着用戶滾動或數據更改(即每次調用reloadData),cellForRowAtIndexPath都可以被調用多次。
我通過跟蹤myLabel.text
保存的先前值設法使其工作。
我在我的viewController上創建了一個NSMutableDictionary
,命名為previousStrings
。 每個鍵都是indexPath.row
,每個值都是該行的字典。
這是來自cellForRow
方法內部的一些代碼。
NSMutableDictionary *strings;
// check to see if previousStrings already has a dictionary at key of indexPath.row
// if not, then create one.
if (![_previousStrings objectForKey:[NSString stringWithFormat:@"%d", indexPath.row]]) {
strings = [[NSMutableDictionary alloc] init];
}
else {
strings = [_previousStrings objectForKey:[NSString stringWithFormat:@"%d", indexPath.row]];
}
然后更改myLabel.text
NSString *cellText = newTextString;
// If the new string is equal to the old string, just set the label without animation
// otherwise run animation and update the value inside the strings dictionary.
if ([strings[@"myLabel"] isEqualToString:newTextString]) {
cell.myLabel.text = newTextString;
}
else {
[cell.myLabel runTextChangeAnimationWithString:newTextString];
[strings setObject:newTextString forKey:@"myLabel"];
}
畢竟,在cellForRow
的末尾,請確保將字符串字典放回previousStrings中
[_previousStrings setObject:strings forKey:[NSString stringWithFormat:@"%d", indexPath.row]];
我希望這對於在同一問題上絆腳石的其他人來說都是有意義的。 如果不夠清晰,請隨時進行編輯並提出問題。
這就是字典的結構。 數字是每個單元格的indexPaths。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.