简体   繁体   English

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

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

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

BTW I also tried explicitly dispatching this in the main queue but it had no effect.顺便说一句,我也尝试在主队列中显式调度它,但没有效果。 It should already be in main queue.它应该已经在主队列中了。

First of all, you don't need to call beginUpdates or endUpdates .首先,您不需要调用beginUpdatesendUpdates Second, you can't animate the change of a label's text value.其次,您不能为标签文本值的变化设置动画。

What you need to is have a label on the cell and initialize the alpha property to 0.0.您需要做的是在单元格上有一个标签并将alpha属性初始化为 0.0。 When accessoryButtonTappedForRowWithIndexPath is called set the alpha property to 1.0 inside of your animation block.当调用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