简体   繁体   English

如何在iPhone屏幕中间显示活动指示灯?

[英]How to display activity indicator in middle of the iphone screen?

When we draw the image, we want to display the activity indicator. 当我们绘制图像时,我们想要显示活动指示器。 Can anyone help us? 有人可以帮助我们吗?

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]     
        initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    activityView.center=self.view.center;
    [activityView startAnimating];
    [self.view addSubview:activityView];

If you are using Swift, this is how you do it 如果您使用的是Swift,这就是您的操作方法

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
activityView.center = self.view.center
activityView.startAnimating()

self.view.addSubview(activityView)

Code updated for Swift 3 代码已更新为Swift 3

在此输入图像描述

Swift code SWIFT代码

import UIKit
class ViewController: UIViewController {

    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)

    @IBOutlet weak var myBlueSubview: UIView!

    @IBAction func showButtonTapped(sender: UIButton) {
        activityIndicator.startAnimating()
    }

    @IBAction func hideButtonTapped(sender: UIButton) {
        activityIndicator.stopAnimating()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // set up activity indicator
        activityIndicator.center = CGPoint(x: myBlueSubview.bounds.size.width/2, y: myBlueSubview.bounds.size.height/2)
        activityIndicator.color = UIColor.yellow
        myBlueSubview.addSubview(activityIndicator)
    }
}

Notes 笔记

  • You can center the activity indicator in middle of the screen by adding it as a subview to the root view rather than myBlueSubview . 您可以将活动指示器置于屏幕中间,方法是将其作为子视图添加到根view而不是myBlueSubview

     activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2) self.view.addSubview(activityIndicator) 
  • You can also create the UIActivityIndicatorView in the Interface Builder. 您还可以在Interface Builder中创建UIActivityIndicatorView Just drag an Activity Indicator View onto the storyboard and set the properties. 只需将活动指示器视图拖到故事板上并设置属性即可。 Be sure to check Hides When Stopped . 务必检查隐藏时的隐藏 Use an outlet to call startAnimating() and stopAnimating() . 使用插座调用startAnimating()stopAnimating() See this tutorial . 请参阅本教程

在此输入图像描述

  • Remember that the default color of the LargeWhite style is white. 请记住, LargeWhite样式的默认颜色是白色。 So if you have a white background you can't see it, just change the color to black or whatever other color you want. 因此,如果您有白色背景,则无法看到它,只需将颜色更改为黑色或任何其他颜色即可。

     activityIndicator.color = UIColor.black 
  • A common use case would be while a background task runs. 一个常见的用例是运行后台任务。 Here is an example: 这是一个例子:

     // start animating before the background task starts activityIndicator.startAnimating() // background task DispatchQueue.global(qos: .userInitiated).async { doSomethingThatTakesALongTime() // return to the main thread DispatchQueue.main.async { // stop animating now that background task is finished self.activityIndicator.stopAnimating() } } 

This is the correct way: 这是正确的方法:

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview: activityIndicator];

[activityIndicator startAnimating];

[activityIndicator release];

Setup your autoresizing mask if you support rotation. 如果支持旋转,请设置自动调整遮罩。 Cheers!!! 干杯!!!

如果您想添加文本以及活动指标,请查看http://nullpointr.wordpress.com/2012/02/27/ios-dev-custom-activityindicatorview/

Try this way 试试这种方式

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.frame = CGRectMake(10.0, 0.0, 40.0, 40.0);
    activityIndicator.center = super_view.center;
    [super_view addSubview: activityIndicator];

[activityIndicator startAnimating];

If you want to center the spinner using AutoLayout, do: 如果要使用AutoLayout使微调器居中,请执行以下操作:

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityView startAnimating];
[self.view addSubview:activityView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:activityView
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterX
                                                     multiplier:1.0
                                                       constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:activityView
                                                      attribute:NSLayoutAttributeCenterY
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterY
                                                     multiplier:1.0
                                                       constant:0.0]];

Swift 3, xcode 8.1 Swift 3,xcode 8.1

You can use small extension to place UIActivityIndicatorView in the centre of UIView and inherited UIView classes: 您可以使用小扩展将UIActivityIndi​​catorView置于UIView的中心并继承UIView类:

Code

extension UIActivityIndicatorView {

    convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
        self.init(activityIndicatorStyle: activityIndicatorStyle)
        center = parentView.center
        self.color = color
        parentView.addSubview(self)
    }
}

How to use 如何使用

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray,  placeInTheCenterOf: view)
activityIndicator.startAnimating()

Full Example 完整的例子

In this example UIActivityIndicatorView placed in the centre of the ViewControllers view: 在此示例中,UIActivityIndi​​catorView位于ViewControllers视图的中心:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray,  placeInTheCenterOf: view)
        activityIndicator.startAnimating()
    }
}

extension UIActivityIndicatorView {

    convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
        self.init(activityIndicatorStyle: activityIndicatorStyle)
        center = parentView.center
        self.color = color
        parentView.addSubview(self)
    }
}

You can set the position like this 您可以像这样设置位置

        UIActivityIndicatorView *activityView = 
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(120, 230, 50, 50);
[self.view addSubview:activityView];

Accordingly change the frame size..... 因此改变框架尺寸.....

For Swift 3 you can use the following: 对于Swift 3,您可以使用以下内容:

 func setupSpinner(){
    spinner = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height:40))
    spinner.color = UIColor(Colors.Accent)
    self.spinner.center = CGPoint(x:UIScreen.main.bounds.size.width / 2, y:UIScreen.main.bounds.size.height / 2)
    self.view.addSubview(spinner)
    spinner.hidesWhenStopped = true
}

Swift 4, Autolayout version Swift 4,Autolayout版本

func showActivityIndicator(on parentView: UIView) {
    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    activityIndicator.startAnimating()
    activityIndicator.translatesAutoresizingMaskIntoConstraints = false

    parentView.addSubview(activityIndicator)

    NSLayoutConstraint.activate([
        activityIndicator.centerXAnchor.constraint(equalTo: parentView.centerXAnchor),
        activityIndicator.centerYAnchor.constraint(equalTo: parentView.centerYAnchor),
    ])
}

?If you want to try to do it by using interface, you can view my video for this . ?如果您想尝试使用界面,可以查看我的视频 This way, you no need to write any code to show Activity Indicator in the middle of the iPhone screen. 这样,您无需编写任何代码来在iPhone屏幕中间显示活动指示器。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textLbl: UILabel!

    var containerView = UIView()
    var loadingView = UIView()
    var activityIndicator = UIActivityIndicatorView()

    override func viewDidLoad() {
        super.viewDidLoad()

        let _  = Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { (_) in
            self.textLbl.text = "Stop ActivitiIndicator"
            self.activityIndicator.stopAnimating()
            self.containerView.removeFromSuperview()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func palyClicked(sender: AnyObject){

        containerView.frame = self.view.frame
        containerView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3)
        self.view.addSubview(containerView)

        loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
        loadingView.center = self.view.center
        loadingView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.5)
        loadingView.clipsToBounds = true
        loadingView.layer.cornerRadius = 10
        containerView.addSubview(loadingView)

        activityIndicator.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        activityIndicator.center = CGPoint(x:loadingView.frame.size.width / 2, y:loadingView.frame.size.height / 2);
        activityIndicator.startAnimating()
        loadingView.addSubview(activityIndicator)

    }
}

轻松解决方案

UIApplication.sharedApplication.networkActivityIndicatorVisible = true/false

Hope this will work: 希望这会奏效:

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

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

I found a way to display activity Indicator with minimum code: 我找到了一种用最少代码显示活动指标的方法:

first of all import your UI Kit 首先导入你的UI工具包

import UIKit;

Then you have to initiate activity indicator 然后你必须启动活动指标

let activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView();

Then just copy paste this functions below which are self explainatory and call them whereever you need them: 然后只需复制粘贴下面的功能,这些功能是自我解释的,并在需要时随时调用它们:

func startLoading(){
    activityIndicator.center = self.view.center;
    activityIndicator.hidesWhenStopped = true;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray;
    view.addSubview(activityIndicator);

    activityIndicator.startAnimating();
    UIApplication.shared.beginIgnoringInteractionEvents();

}

func stopLoading(){ 

    activityIndicator.stopAnimating();
    UIApplication.shared.endIgnoringInteractionEvents();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM