Mimic UIAlertView Bounce?

前端 未结 4 1878
面向向阳花
面向向阳花 2020-12-23 02:15

Whats the best way to mimic the bouncing animation from the UIAlertView on the iPhone? Is there some built-in mechanism for this? The UIAlertView itself won\'t work for my n

4条回答
  •  难免孤独
    2020-12-23 03:06

    Here's how I did it for an app I'm working on. The effect I was going for was bouncing when you pressed the view. Experiment with the values to suit your taste and the desired speed of the effect.

    - (void) bounceView:(UIView*)bouncer
    {
        // set duration to whatever you want
        float duration = 1.25;
        // use a consistent frame rate for smooth animation.
        // experiment to your taste
        float numSteps = 15 * duration;
    
        // scale the image up and down, halving the distance each time
        [UIView animateKeyframesWithDuration:duration
                                       delay:0
                                     options:UIViewKeyframeAnimationOptionCalculationModeCubic
                                  animations:^{
                                      float minScale = 0.50f; // minimum amount of shrink
                                      float maxScale = 1.75f; // maximum amount of grow
                                      for(int i = 0; i< numSteps*2; i+=2)
                                      {
                                          // bounce down
                                          [UIView addKeyframeWithRelativeStartTime:duration/numSteps * i
                                                                  relativeDuration:duration/numSteps
                                                                        animations:^{
                                                                            bouncer.layer.transform = CATransform3DMakeScale(minScale, minScale, 1);
                                                                        }];
                                          // bounce up
                                          [UIView addKeyframeWithRelativeStartTime:duration/numSteps * (i+1)
                                                                  relativeDuration:duration/numSteps
                                                                        animations:^{
                                                                            bouncer.layer.transform = CATransform3DMakeScale(maxScale, maxScale, 1);
                                                                        }];
    
                                          // cut min scale halfway to identity
                                          minScale = minScale + (1.0f - minScale) / 2.0f;
                                          // cut max scale halfway to identity
                                          maxScale = 1.0f + (maxScale - 1.0f) / 2.0f;
                                      }
                                  } completion:^(BOOL finished) {
                                      // quickly smooth out any rounding errors
                                      [UIView animateWithDuration:0.5*duration/numSteps animations:^{
                                          bouncer.layer.transform = CATransform3DIdentity;
                                      }];
                                  }];
    }
    

提交回复
热议问题