问题描述
我正在加载一个 UIWebView
,同时我不想用这个显示一个空白页面 活动指示器旋转(siri 活动指示器).据我了解,您不能更改图像,但我不能使用该图像并创建一个旋转 360° 并循环播放的动画吗?还是会耗尽电池?
I am loading a UIWebView
and in the meantime I wan't to show a blank page with this 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?
像这样?:
- (void)webViewDidStartLoad:(UIWebView *)webView {
//set up animation
[self.view addSubview:self.loadingImage];
//start animation
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//stop animation
[self.loadingImage removeFromSuperview];
}
我该怎么办?
提前致谢!
推荐答案
大部分都可以在 Stack Overflow 中找到.让我总结一下:
Most of this is found in Stack Overflow. Let me summarize:
创建一个用作活动指示器的 UIImageView(在情节提要场景、NIB、代码......任何你想要的地方).我们称之为 _activityIndicatorImage
Create an UIImageView which will serve as an activity indicator (inside storyboard scene, NIB, code ... wherever you wish). Let's call it _activityIndicatorImage
加载你的图片:_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"];
}
在我的 layoutSubviews 方法中,我启动了旋转.如果这更适合您的情况,您可以将其放在 webViewDidStartLoad
和 webViewDidFinishLoad
中:
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];
}
您总是可以使用 [_activityIndicatorImage.layer removeAllAnimations];
这篇关于带有自定义图像的活动指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!