xcode 如何在视图中显示加载动画 - IOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11947164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:14:04  来源:igfitidea点击:

How to show loading animation in a view - IOS

iphoneobjective-ciosxcode

提问by Feroz

I have a view in which I want to show loading animation. I have seen some application they are showing circular image to show loading, and the action will happen on background, Same thing I want to achieve here, Any inbuilt animation is available in IOS?

我有一个视图,我想在其中显示加载动画。我看过一些应用程序,他们显示圆形图像以显示加载,并且该操作将在后台发生,我想在这里实现同样的事情,IOS 中有任何内置动画可用吗?

TIA

TIA

回答by Roma-MT

You can use the built in activity indicator.

您可以使用内置的活动指示器。

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2 , (alert.bounds.size.height) /2);
[indicator startAnimating];

simply add it as a subview in to your view.

只需将其作为子视图添加到您的视图中。

回答by Bourne

You may use the UIActivityIndicator if you want to keep things simple. Or there are plenty of open source activity indicators that do a lot of fancy stuff in addition to just showing a spinning wheel. MBProgressHUDand SVProgressHUDare two neat implementations.

如果您想让事情变得简单,您可以使用 UIActivityIndi​​cator。或者有很多开源活动指标,除了只显示一个旋转轮之外,还可以做很多花哨的事情。MBProgressHUDSVProgressHUD是两个巧妙的实现。

回答by Mohamad Chami

Create YourViewController, and then add the the MBProgressHUB library to your project (you can get the library from here); download the project and move the library to your project.

创建 YourViewController,然后将 MBProgressHUB 库添加到您的项目中(您可以从这里获取该库);下载项目并将库移动到您的项目中。

Then you can use the following code to achieve your task:

然后您可以使用以下代码来完成您的任务:

YourViewController.h

你的视图控制器.h

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"

@interface YourViewController : UITableViewController <MBProgressHUDDelegate>
{
    MBProgressHUD *hud;
}

YourViewController.m

YourViewController.m

#import "YourViewController.h"
@interface YourViewController ()
@end

@implementation YourViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeProgressLoading];
    [self getObjects];
    [hud hide:YES afterDelay:1];
}

-(void) initializeProgressLoading {
    hud = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:hud];
    hud.delegate = self;
    hud.labelText = @"Loading";
    [hud showWhileExecuting:@selector(sleep) onTarget:self withObject:nil animated:YES];
}

- (void)sleep {
    sleep(50000);
}

- (void) getObjects {
// connect to db and get all objects
//you can write any thing here
}

- (void)hudWasHidden:(MBProgressHUD *)hud1 {
    // Remove HUD from screen when the HUD was hidded
    [hud removeFromSuperview];
    hud = nil;
}