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];
这篇关于带有自定义图像的活动指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有自定义图像的活动指示器
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01