UITableviewcell animation only once

2019-06-08 02:10发布

Here is my willDisplayCell animation code I want to animate only once, don't repeat when scroll up the tableview.

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

    UIView *cellContentView = [cell contentView];
    CGFloat rotationAngleDegrees = -30;
    CGFloat rotationAngleRadians = rotationAngleDegrees * (M_PI/180);
    CGPoint offsetPositioning = CGPointMake(0, cell.contentView.frame.size.height*4);
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DRotate(transform, rotationAngleRadians, -50.0, 0.0, 1.0);
    transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, -50.0);
    cellContentView.layer.transform = transform;
    cellContentView.layer.opacity = 0.8;

    [UIView animateWithDuration:0.95 delay:00 usingSpringWithDamping:0.85 initialSpringVelocity:0.8 options:0 animations:^{
        cellContentView.layer.transform = CATransform3DIdentity;
        cellContentView.layer.opacity = 1;
    } completion:^(BOOL finished) {}];

    }

2条回答
混吃等死
2楼-- · 2019-06-08 02:20

In order to animate each cell only once each cell has to know that it animated itself and it shouldn't again. Easiest way to achieve that is to give the cell BOOL variable called animated to determine whether if or not the cell has already animated.

Create custom cell class

Create new Cocoa Class called for example CustomCell. It should subclass the UITableViewCell.

Inside CustomCell.h declare a BOOL property called animated:

@property (nonatomic) BOOL animated;

If you are using storyboards like me, remember to also update the class of a cell in storyboard editor, just like you do when you are creating new view controllers

That's it here.

Modify View Controller

Now you should import your new class and swap (UITableViewCell *) with (CustomCell *) in your tableViews delegate methods.

In willDisplayCell: add necessary logic, it will look like this:

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

    if(!cell.animated){ //added

        cell.animated = YES;  //added    

        UIView *cellContentView = [cell contentView];
        CGFloat rotationAngleDegrees = -30;
        CGFloat rotationAngleRadians = rotationAngleDegrees * (M_PI/180);
        CGPoint offsetPositioning = CGPointMake(0, cell.contentView.frame.size.height*4);
        CATransform3D transform = CATransform3DIdentity;
        transform = CATransform3DRotate(transform, rotationAngleRadians, -50.0, 0.0, 1.0);
        transform = CATransform3DTranslate(transform, offsetPositioning.x, offsetPositioning.y, -50.0);
        cellContentView.layer.transform = transform;
        cellContentView.layer.opacity = 0.8;

        [UIView animateWithDuration:0.95 delay:00 usingSpringWithDamping:0.85 initialSpringVelocity:0.8 options:0 animations:^{
            cellContentView.layer.transform = CATransform3DIdentity;
            cellContentView.layer.opacity = 1;
        } completion:^(BOOL finished) {}];

    }

And this is it. Hope it works for you

查看更多
做个烂人
3楼-- · 2019-06-08 02:34

If you want any of your code to run only once, you can use dipatch_once. This will make sure that this code is run only once in life span of parent object. You can put that code inside dispatch_async if that code is being run from somewhere other than tableview's event handlers.

dispatch_async(dispatch_get_main_queue(),^{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
           //Your animation code.
    });
};
查看更多
登录 后发表回答