IOS:强制tableView单元在点击时动画化更改

问题描述 投票:0回答:1

[我正在尝试点击附件视图时在单元格上执行动画。点击的委托方法正在触发,我可以让该行执行某些操作(更改标签),但是它忽略了动画(或者在其他情况下,甚至不进行更改。)如何使动画正常工作?

- (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");
                             }];     
    }

BTW我也尝试在主队列中显式调度此事件,但是它没有任何效果。它应该已经在主队列中。

ios uitableview animation
1个回答
0
投票

首先,您不需要呼叫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;
    }];
}
© www.soinside.com 2019 - 2024. All rights reserved.