Duck
Duck

Reputation: 35933

UIView Animation is far from smooth

I am trying to scale a button up and down to call attention to it.

I have tried two animation methods:

method 1

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.autoreverses = YES;
animation.repeatDuration = 2;
animation.duration = 0.4;
animation.fromValue = @(1.0f);
animation.toValue= @(1.2f);
[button.layer addAnimation:animation forKey:@"scale"];

method 2

  CGAffineTransform scaleUp = CGAffineTransformMakeScale(1.2f, 1.2f);
  [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAutoreverse
                   animations:^{

                     [button setTransform:scaleUp];

                   } completion:^(BOOL finished) {

                   }];

Same problem with both animations. Near the end, when the button is scaling down to its normal scale, the animation jumps directly to scale 1.0.

Something like:

1.2 ... 1.18 ... 1.15 ... boom ... 1.0

and I see the button popping from one huge scale value to 1, instead of being smooth.

Upvotes: 0

Views: 331

Answers (1)

Leo
Leo

Reputation: 24714

You need to set the final state of the button.In your case,the scale change like

1.0->1.1->1.2->1.1->1.0 ->1.2(jump to final state)

Gif

enter image description here

CGAffineTransform scaleUp = CGAffineTransformMakeScale(1.5f, 1.5f);
[UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAutoreverse
                 animations:^{

                     [self.button setTransform:scaleUp];

                 } completion:^(BOOL finished) {
                     self.button.transform = CGAffineTransformIdentity;
                 }];

Upvotes: 2

Related Questions