带有自定义图像的活动指示 [英] Activity indicator with custom image
问题描述
我正在加载一个 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];
这篇关于带有自定义图像的活动指示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!