如何以编程方式在视图中间创建活动指示器?

时间:2011-02-24 10:15:13

标签: ios objective-c cocoa-touch uiactivityindicatorview

我正在使用在线解析Feed的应用。当我单击刷新按钮时,需要一些时间来重新解析文件并显示其数据。当我点击刷新按钮时,我想在视图中间有一个活动指示器。并且在完成解析时该指示符应该隐藏。 我正在使用此代码,但它不起作用。

- (IBAction)refreshFeed:(id)sender
{
   UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
   [self.view addSubview:spinner];

   [spinner startAnimating];

   // parsing code code

   [spinner release];
}

14 个答案:

答案 0 :(得分:17)

UIActivityIndi​​cator通常需要放入与长进程(解析订阅源)不同的线程中才能显示。

如果要将所有内容保存在同一个线程中,则需要指示时间显示。 This Stackoverflow question解决了代码中的延迟问题。

编辑:两年后,我认为,无论何时你使用延迟来制造某些东西,你都可能做错了。以下是我现在要执行此任务的方法:

- (IBAction)refreshFeed:(id)sender {
    //main thread
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [self.view addSubview:spinner];

    //switch to background thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        //back to the main thread for the UI call
        dispatch_async(dispatch_get_main_queue(), ^{
            [spinner startAnimating];
        });
        // more on the background thread

        // parsing code code

        //back to the main thread for the UI call
        dispatch_async(dispatch_get_main_queue(), ^{
            [spinner stopAnimating];
        });
    });
}

答案 1 :(得分:12)

  

我想我无法解释它   well.ok再试一次。它是一个导航   基于应用程序我有tableView。一切   单元格创建一个detailView。在RootView上   这是一个tableview有一个   刷新按钮。当我点击它   按钮它再次解析Feed。和   这需要一些时间。那个时候   程序没有回应。和解析是   完成它再次工作。现在我需要   当时的活动指标。一世   不知道如何添加xib。 bcz什么时候   我打开main.xib并放置活动   RootViewController中的指示器。它   来自整个tableView的前面。现在   可能我解释得很好。如果不让我   我知道我会再试一次。

从上面的内容来看,程序在解析期间没有响应,这是一个问题。如果在解析数据时GUI冻结,则应将该操作移动到辅助线程。这样,您的GUI仍然可以响应,您将能够看到活动指示器。

在主线程上你应该有类似的东西,以显示活动指示器:

UIActivityIndicatorView  *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
av.frame = CGRectMake(round((yourView.frame.size.width - 25) / 2), round((yourView.frame.size.height - 25) / 2), 25, 25);
av.tag  = 1;
[yourView addSubview:av];
[av startAnimating];

在辅助线程完成后,这是你解析数据的线程,你应该在主线程上调用这样的东西来删除活动指示器:

UIActivityIndicatorView *tmpimg = (UIActivityIndicatorView *)[yourView viewWithTag:1];
[tmpimg removeFromSuperview];

答案 2 :(得分:7)

将活动指示器的中心更改为

activityIndicator.center = self.view.center;

答案 3 :(得分:7)

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.alpha = 1.0;
[collectionVwSearch addSubview:activityIndicator];
activityIndicator.center = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, [[UIScreen mainScreen]bounds].size.height/2);
[activityIndicator startAnimating];//to start animating

对于Swift

let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
activityIndicator.alpha = 1.0
collectionVwSearch.addSubview(activityIndicator)
activityIndicator.center = CGPointMake(UIScreen.mainScreen().bounds.size.width / 2, UIScreen.mainScreen().bounds.size.height / 2)
activityIndicator.startAnimating()

停止活动指标在适当的地方写下[activityIndicator stopAnimating]

答案 4 :(得分:2)

self.activity.center = CGPointMake(self.view.bounds.size.width / 2.0f, self.view.bounds.size.height / 2.0f);
self.activity.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);

答案 5 :(得分:1)

在开始任务之前添加此代码:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:spinner];
[spinner setFrame:CGRectMake((self.view.frame.size.width/2)-(spinner.frame.size.width/2), (self.view.frame.size.height/2)-(spinner.frame.size.height/2), spinner.frame.size.width, spinner.frame.size.height)];
[spinner startAnimating];    

完成任务后添加:

[spinner stopAnimating];

答案 6 :(得分:1)

在Swift中,这适用于我的应用

let activity_view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
activity_view.frame =  CGRectMake(0.0, 0.0, 40.0,40.0)
activity_view.center = self.view.center
self.view.addSubview(activity_view)
activity_view.bringSubviewToFront(self.view)
activity_view.startAnimating()

答案 7 :(得分:0)

/ create activity indicator 
activityIndicator = [[UIActivityIndicatorView alloc] 
    initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];

...
 [self.view addSubview:activityIndicator];
// release it
[activityIndicator release];

更好的方法是在xib&上添加指标根据您的要求隐藏/取消隐藏

答案 8 :(得分:0)

spinner.center = self.view.center;

要从rootController显示微调器,请尝试:

[self.navigationController.visibleViewController.view addSubview:spinner];

[self.navigationController.view addSubview:spinner];

答案 9 :(得分:0)

试试这个

activityIndicator.center = CGPointMake(160,240);

答案 10 :(得分:0)

其他一些答案的问题是每次都会添加一个新的活动指示器,以便子视图开始堆积。以下是仅使用单个UIActivityIndicator的完整示例。

enter image description here

这是上述项目的完整代码:

import UIKit
class ViewController: UIViewController {

    // only using a single spinner instance
    let spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)

    override func viewDidLoad() {
        super.viewDidLoad()

        // setup spinner
        spinner.backgroundColor = UIColor(white: 0, alpha: 0.2) // make bg darker for greater contrast
        self.view.addSubview(spinner)
    }

    @IBAction func startSpinnerButtonTapped(sender: UIButton) {

        // start spinner
        spinner.frame = self.view.frame // center it
        spinner.startAnimating()

        let qualityOfServiceClass = QOS_CLASS_BACKGROUND
        let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
        dispatch_async(backgroundQueue, {       

            // do something that takes a long time
            sleep(2)

            // stop spinner when background task finishes
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.spinner.stopAnimating()
            })
        })
    }
}

答案 11 :(得分:0)

你可以写swift 4

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)

override func viewDidLoad() {
    super.viewDidLoad()

    spinner.backgroundColor = UIColor(white: 0, alpha: 0.2) // make bg darker for greater contrast
    self.view.addSubview(spinner)
}

@IBAction func foodActionBtn(_ sender: Any) {

    // start spinner
    spinner.frame = self.view.frame // center it
    spinner.startAnimating()

    let qualityOfServiceClass = QOS_CLASS_BACKGROUND
    let backgroundQueue = DispatchQueue.global(qos: .background)
    backgroundQueue.async(execute: {
        sleep(2)
        self.spinner.stopAnimating()
    })
}

答案 12 :(得分:0)

SWIFT 5

声明

let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.medium)
//if you want a bigger activity indicator, change style from .medium to .large

ViewDidLoad
activityIndicator.alpha = 1.0
view.addSubview(activityIndicator)
activityIndicator.center = view.center

激活活动指示器,在我的情况下,它在`didSelectRowAt`方法中
activityIndicator.startAnimating()

如果您在动画处于活动状态时推入另一个viewController,然后弹出,则仍会看到活动指示器处于动画状态,因此您应该这样做

override func viewWillAppear(_ animated: Bool) {
        if activityIndicator.isAnimating {
            activityIndicator.stopAnimating()
        }
    }

答案 13 :(得分:-2)

Poscean先生我的代码正在运作。但是你想要显示大型活动指示器,你可以使用它....

var activityIndi​​cator = UIActivityIndi​​catorView()     activityIndi​​cator = UIActivityIndi​​catorView(activityIndi​​catorStyle:UIActivityIndi​​catorViewStyle.gray)     activityIndi​​cator.frame = CGRect(x:0,y:0,width:50,height:50)     let transform:CGAffineTransform = CGAffineTransform(scaleX:1.5,y:1.5)     activityIndi​​cator.transform = transform     activityIndi​​cator.center = self.view.center     activityIndi​​cator.startAnimating()     self.view.addSubview(activityIndi​​cator)