iOS Animation A Method for Timing the Flip and Jitter of UIView

  • 2021-12-19 07:03:12
  • OfStack

(Flip) Mode 1:


[NSTimer scheduledTimerWithTimeInterval:3.f repeats:YES block:^(NSTimer * _Nonnull timer) {
      CABasicAnimation* rotationAnimation = [CABasicAnimation animation];;
      rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
      rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
      rotationAnimation.duration = 1;
      //  Switch the interface to ensure that the animation does not stop 
      rotationAnimation.removedOnCompletion = NO;
      rotationAnimation.repeatCount = 1;
      [self.bindCardImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }];

(Flip) Mode 2 (this mode is better):


CABasicAnimation *waitAnimation = [CABasicAnimation animation];
    waitAnimation.toValue = [NSNumber numberWithFloat:1.0];
    waitAnimation.duration = 3.f;
    waitAnimation.beginTime = 3.f;

    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1.f;

    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 4.f;
    group.repeatCount = CGFLOAT_MAX;
    group.removedOnCompletion = NO;

    [group setAnimations:@[waitAnimation, rotationAnimation]];

    [self.bindCardImageView.layer addAnimation:group forKey:@"bindCardImageViewAnimation"];

Jitter:


CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  // Set the amplitude of jitter 
  shake.fromValue = [NSNumber numberWithFloat:-0.2];
  shake.toValue = [NSNumber numberWithFloat:+0.2];
  shake.duration = 0.1;
  shake.autoreverses = YES; // Duplicate or not 
  shake.repeatCount = 3;

  [itemView.iconImageView.layer addAnimation:shake forKey:@"imageView"];


Related articles: