UITableViewCell的自定义行动画

时间:2014-05-29 08:43:32

标签: ios xcode animation uitableview reloaddata

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

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

但要设置我自己的动画。如果这个动画将在UITableViewCell基础上,那将是完美的,因为我需要为单元格中的一些元素设置动画。

我尝试做什么:

我有像这样的自定义单元格

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

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

马尔科

3 个答案:

答案 0 :(得分:1)

在.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

答案 1 :(得分:0)

我认为你必须有自定义的UITableViewCell类,你必须实现类似的方法,

- (void)swipeLabel2Out {

    UIView animateWithDuration:1.0 animations:^{

        // set the frame of your label2

    } completion:^(BOOL finished) {

        // hide your label2
    }
}

您可以使用

方法轻松跟踪您的手机
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

并调用[cell swipeLabel2Out];方法

答案 2 :(得分:0)

希望这会对您有所帮助

 - (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier%d",indexPath.row];

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier];

        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        label = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 100, 50)];
        label.backgroundColor = [UIColor blueColor];
        label.text = @"Test";
        label.tag = indexPath.row;
        [label setUserInteractionEnabled:YES];
        [cell.contentView addSubview:label];
        [arrayLabel addObject:label];


        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedGesture:)];
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        swipe.numberOfTouchesRequired = 1;
        [label addGestureRecognizer:swipe];

        }

        return cell;
    }

    - (void)swipedGesture:(UIGestureRecognizer *)recognizer
    {

        [UIView animateWithDuration:1
                         animations:^{
                             CGRect rect = label.frame;
                             rect.origin.x = 320;
                             label.frame = rect;
                         }
                         completion:^(BOOL finished){
                             NSLog(@"completion block");
                         }];

    }