didSelectRowAtIndexPath中的文本动画:?

时间:2013-10-20 18:55:55

标签: ios objective-c animation uitableview didselectrowatindexpath

我正在使用MCSwipeTableViewCell,因此我们的想法是用户向左或向右滑动它以完成操作。但是,用户可能并不总是熟悉此操作,所以当他们点击单元格(期望选择它)时,我希望文本短暂闪烁一条简短的指令然后返回原始文本。

我已经在这几天了,并且一直没有运气回来。我已经尝试使用完成块,玩延迟,所有种类但动画似乎很快完成你实际上没有看到变化,我做错了什么?

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

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    MCSwipeTableViewCell *cell = (MCSwipeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    Station *station = self.recentStations[indexPath.row];

    [UIView animateWithDuration:0.5 animations:^{

        //code to fade the text out
        CATransition *animation = [CATransition animation];
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.type = kCATransitionFade;
        animation.duration = 0.35;
        [cell.textLabel.layer addAnimation:animation forKey:@"kCATransitionFade"];

        //instructions to appear briefly
        cell.textLabel.text = @"Swipe left to set alarm or right to remove";


        //nested animation to revert text after a 1.5 second delay
       [UIView animateWithDuration:.05 delay:1.5 options:UIViewAnimationOptionCurveEaseIn animations:^{

           //code to fade the text back in
           CATransition *animation = [CATransition animation];
           animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
           animation.type = kCATransitionFade;
           animation.duration = 0.35;
           [cell.textLabel.layer addAnimation:animation forKey:@"kCATransitionFade"];
           cell.textLabel.text = station.name;

       } completion:nil];

    }];

}

1 个答案:

答案 0 :(得分:0)

您正在使用重复的嵌套动画API。试试下面的代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    MCSwipeTableViewCell *cell = (MCSwipeTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    Station *station = self.recentStations[indexPath.row];

    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        cell.textLabel.alpha = 0.0f;              
    } completion:^(BOOL finished) {
        //instructions to appear briefly
        cell.textLabel.alpha = 1.0f;
        cell.textLabel.text = @"Swipe left to set alarm or right to remove";

        [UIView animateWithDuration:0.5 delay:1.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            cell.textLabel.alpha = 0.0f;
        } completion:^(BOOL finished) {
            cell.textLabel.alpha = 1.0f;
            cell.textLabel.text = station.name;
        }];
    }];
}

该代码将使新文本立即换出旧文本并显示 - 您可以想出如何添加更多动画以防止这种情况发生。