Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity indicator with custom image

I am loading a UIWebView and in the meantime I wan't to show a blank page with thisspinner activity indicator spinning (siri activity indicator). From what I have understand you can not change the image, but can't I use that image and create an animation with it rotating 360° and looping? Or will that drain the battery?

something like this?:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    //set up animation        
    [self.view addSubview:self.loadingImage];
    //start animation
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{   
    //stop animation
    [self.loadingImage removeFromSuperview];
}

What should I do?

Thanks in advance!

like image 439
Tibbe Avatar asked Jan 18 '14 16:01

Tibbe


People also ask

How do I add a custom activity indicator in react native?

You can create your own activity indicator by animating View styles with the Animated API. If you need to draw more advanced animated shapes, and View styles don't fulfill your design requirements , I recommend using the react-native-svg library.


2 Answers

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];

like image 149
demosten Avatar answered Sep 20 '22 09:09

demosten


You may use this beautiful loader inspired from Tumblr app:
Asich/AMTumblrHud

like image 20
Almas Adilbek Avatar answered Sep 18 '22 09:09

Almas Adilbek