簡體   English   中英

IOS:強制 tableView 單元格在點擊時動畫更改

[英]IOS: Force tableView cell to animate change on tap

我正在嘗試在點擊附件視圖時在單元格上執行動畫。 tapped 委托方法正在觸發,我可以讓該行執行某些操作——更改標簽,但它忽略了動畫(或者在另一種情況下——甚至沒有進行更改。)如何讓動畫正常工作?

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    MyCustomCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

                    [UIView animateWithDuration:5.0
                                    delay: 5.0
                                    options: UIViewAnimationOptionCurveEaseIn
                                                                 animations:^{
//NEXT TWO LINES HAVE NO EFFECT ON CELL SO COMMENTED OUT
//cell.nameLabel.text = @"Thank you. FIRST ANIMATE TRY";                              
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//NEXT THREE LINES CHANGE TEXT BUT WITHOUT ANIMATION
[self.tableView beginUpdates];
cell.nameLabel.text = @"Thank you. Second try!";
[self.tableView endUpdates];                                                                                
                             }
                             completion:^(BOOL finished){
                                 NSLog(@"animation finished");
                             }];     
    }

順便說一句,我也嘗試在主隊列中顯式調度它,但沒有效果。 它應該已經在主隊列中了。

首先,您不需要調用beginUpdatesendUpdates 其次,您不能為標簽文本值的變化設置動畫。

您需要做的是在單元格上有一個標簽並將alpha屬性初始化為 0.0。 當調用accessoryButtonTappedForRowWithIndexPath時,在動畫塊內將 alpha 屬性設置為 1.0。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AwesomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.thankYouLabel.text = @"Thank you";
    cell.thankYouLabel.alpha = 0.0;
    return cell;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    AwesomeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [UIView animateWithDuration:1.0 animations:^{
        cell.thankYouLabel.alpha = 1.0;
    }];
}

暫無
暫無

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

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