简体   繁体   中英

When invoked within an animation block, a UIView animation fails to repeat indefinitely

I've created my own version of a UIActivityIndicatorView , which spins a UIImageView indefinitely while it is active. The problem is that if the animation (code below) is started from within another animation block (eg, while a view is being pushed onto the navigation stack), the animation is canceled and the spinner remains in the non-animating state.

- (void) spin: (UIViewAnimationOptions) options {
  [UIView animateWithDuration: 0.5f
                        delay: 0.0f
                      options:options
                   animations: ^{
                     self.transform = CGAffineTransformRotate(self.transform, M_PI / 2);
                   } completion: ^(BOOL finished) {
                     if (finished) {
                       if (self.isAnimating) {
                         [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                       } else if (options != UIViewAnimationOptionCurveEaseOut) {
                         [self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                       }
                     }
                   }];
}


- (void) startAnimating {
  if (!self.isAnimating) {
    self.isAnimating = YES;
    [self spin: UIViewAnimationOptionCurveEaseIn];
  }
}

One possible solution is to insert an artificial delay before invoking spin: for the first time (ie, performSelector:withOptions:afterDelay ), but this is an ugly hack.

Is there any way to ensure that this spin does not get canceled by an outer animation block, or perhaps a better way to approach it all together?

According to the UIView animation guide:

By default, nested animations do inherit the parent's duration and animation curve but even those options can be overridden as needed. The UIViewAnimationOptionOverrideInheritedCurve and UIViewAnimationOptionOverrideInheritedDuration keys used in the nested animation block allow the curve and duration values from the first animation to be modified for the second animation. If these keys were not present, the duration and curve of the outer animation block would be used instead.

Pass these options in to override the inherited properties from the outer animation block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM