Start animation one times for each cell

51 views Asked by At

I create animation with scale button when user make edit mode. Animation start but, when I scroll cells it is repeat.

Any suggestion?

Conde in drawRect

CABasicAnimation *fullRotation1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

        fullRotation1.fromValue = [NSNumber numberWithFloat:0];
        fullRotation1.toValue = [NSNumber numberWithFloat:1];
        fullRotation1.duration = 0.3;
        fullRotation1.repeatCount = 1;
        fullRotation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

        [self.deleteButton.layer addAnimation:fullRotation1 forKey:@"scale"];
1

There are 1 answers

0
Guy Kogus On

drawRect is not where you should be calling an animation function. Try something more like this:

@interface MyTableViewCell : UITableViewCell

@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, assign) BOOL animationPlayed;

- (void)playAnimation;

@end

@implementation MyTableViewCell

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if (editing)
    {
        [self playAnimation];
    }
}

- (void)playAnimation
{
    if (self.animationPlayed == NO)
    {
        self.animationPlayed = YES;

        self.deleteButton.transform = CGAffineTransformMakeScale(0.0, 0.0);
        [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.deleteButton.transform = CGAffineTransformMakeScale(1.0, 1.0);
                         }
                         completion:nil];
    }
}

@end