iOS:强制tableView单元在点击时动画化更改 [英] IOS: Force tableView cell to animate change on tap

查看:149
本文介绍了iOS:强制tableView单元在点击时动画化更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to perform an animation on a cell when the accessory view is tapped. The tapped delegate method is firing and I can get the row to do something--change label, but it is ignoring the animation (or in another case--not even making the change.) How can I get the animation to work properly?

- (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我也尝试在主队列中显式分派此命令,但没有效果。它应该已经在主队列中。

BTW I also tried explicitly dispatching this in the main queue but it had no effect. It should already be in main queue.

推荐答案

首先,您不需要调用 beginUpdates endUpdates 。其次,您不能为标签文本值的变化设置动画。

First of all, you don't need to call beginUpdates or endUpdates. Second, you can't animate the change of a label's text value.

您需要做的是在单元格上放置标签并初始化 alpha 属性设置为0.0。调用 accessoryButtonTappedForRowWithIndexPath 时,请将动画块内的alpha属性设置为1.0。

What you need to is have a label on the cell and initialize the alpha property to 0.0. When accessoryButtonTappedForRowWithIndexPath is called set the alpha property to 1.0 inside of your animation block.

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

这篇关于iOS:强制tableView单元在点击时动画化更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆