ios - UITableViewCell 的自定义行动画

标签 ios xcode animation uitableview reloaddata

当我需要刷新 UITableView 时,我需要一些自定义动画。我在寻找类似的东西:

[self.table reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];

但要设置我自己的动画。如果这个动画基于 UITableViewCell 就完美了,因为我需要单独为单元格中的一些元素设置动画。

我尝试做的事情:

我有这样的自定义单元格

 ___________________________________
|  lable   |separator|  lable 2     |
 -----------------------------------

我希望标签 2 向左或向右滑动,但不是整个单元格。

马可

最佳答案

在 .h 中,您将拥有 reloadInWithMyOwnAnimation BOOL 变量。在重新加载该部分之前,将其设置为 YES。重新加载后将其设置为 NO。

    reloadingWithMyOwnAnimation = YES;

    [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];

    reloadingWithMyOwnAnimation = NO;



    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *myCustomCell = cell;

        BOOL swipeLeft = YES;


        if (reloadingWithMyOwnAnimation == YES) {


            [UIView animateWithDuration:0.8f animations:^{

                if (swipeLeft) {
                    //Swipe Right

                    //Get the label frame
                    CGRect myLabelFrame = myCustomCell.textLabel.frame;


                    //Set it to 0
                    myLabelFrame.origin.x = 0;

                    //Move it to Right
                    myLabelFrame.origin.x += cell.bounds.size.width;


                    //Set the frame of the label
                    [myCustomCell.textLabel setFrame:myLabelFrame];


                }
                else{
                    //Swipe Left

                    //Get the label frame
                    CGRect myLabelFrame = myCustomCell.textLabel.frame;


                    //Set it to 0
                    myLabelFrame.origin.x = 0;

                    //Move it to Left
                    myLabelFrame.origin.x -= cell.bounds.size.width;


                    //Set the frame of the label
                    [myCustomCell.textLabel setFrame:myLabelFrame];



                }

                [myCustomCell layoutSubviews];


            } completion:^(BOOL finished) {

                NSLog(@"Done");


            }];


        }
}

enter image description here

关于ios - UITableViewCell 的自定义行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23929212/

相关文章:

iOS - iOS 8 中的 UIAlertView/UIAlertController 方向问题

ios - 从 CGAffineTransform 获取比例和旋转角度?

c++ - 请推荐一个适用于XP、Win7、Win8的二维动画c++工具

ios - 处理触摸而不消耗它

ios - 仅设备上的 RestSharp/MonoTouch 错误

ios - 命令/bin/sh 失败,退出代码为 64 Xcode 8

java - 控制 2d 游戏的动画速度

ios - 如何安全地将对象存储在 ARC 下的 countByEnumeratingWithState 中?

ios - Xcode不验证构建

objective-c - Interface Builder 有没有办法渲染不覆盖 drawRect : 的 IBDesignable View