Activity indicator with custom image

后端 未结 10 1297
甜味超标
甜味超标 2021-01-31 05:58

I am loading a UIWebView and in the meantime I wan\'t to show a blank page with this\"spinner\" activity indic

10条回答
  •  太阳男子
    2021-01-31 06:44

    Most of this is found in Stack Overflow. Let me summarize:

    Create an UIImageView which will serve as an activity indicator (inside storyboard scene, NIB, code ... wherever you wish). Let's call it _activityIndicatorImage

    Load your image: _activityIndicatorImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"activity_indicator"]];

    You need to use animation to rotate it. Here is the method I use:

    + (void)rotateLayerInfinite:(CALayer *)layer
    {
        CABasicAnimation *rotation;
        rotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotation.fromValue = [NSNumber numberWithFloat:0];
        rotation.toValue = [NSNumber numberWithFloat:(2 * M_PI)];
        rotation.duration = 0.7f; // Speed
        rotation.repeatCount = HUGE_VALF; // Repeat forever. Can be a finite number.
        [layer removeAllAnimations];
        [layer addAnimation:rotation forKey:@"Spin"];
    }
    

    Inside my layoutSubviews method I initiate rotation. You could place this in your webViewDidStartLoad and webViewDidFinishLoad if this is better for your case:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        // some other code 
    
        [Utils rotateLayerInfinite:_activityIndicatorImage.layer];
    }
    

    You could always always stop rotation using [_activityIndicatorImage.layer removeAllAnimations];

提交回复
热议问题