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?

137741 次浏览
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];


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

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];

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.....

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!!!

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];

If you want to add a text along with activity indicator please look at http://nullpointr.wordpress.com/2012/02/27/ios-dev-custom-activityindicatorview/

如果你想尝试通过使用接口,你可以 看我的视频。 这样,您就不需要编写任何代码来显示位于 iPhone 屏幕中间的 Activity Indicator。

如果你正在使用 Swift,这就是你如何做到这一点

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


self.view.addSubview(activityView)

如果您想使用自动布局使旋转器居中,请:

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的代码

enter image description here

快速密码

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)
}
}

笔记

  • 您可以将活动指示器作为子视图添加到根 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. 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(). See this tutorial.

enter image description here

  • Remember that the default color of the LargeWhite style is white. 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()
    }
    }
    

斯威夫特3号,代码8.1

您可以使用小扩展放置 位于 UIView 中心的 UIActivityIndicatorView和继承的 UIView 类:

密码

extension UIActivityIndicatorView {


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

怎么用

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

完整例子

在此示例中,位于 ViewController 视图中心的 UIActivityIndicatorView:

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)
}
}

我找到了一种用最少代码显示活动指示器的方法:

首先导入 UI 工具包

import UIKit;

然后你必须启动活动指示器

let activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView();

然后复制粘贴下面这些自我解释的函数,并在任何需要的地方调用它们:

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();


}
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)


}
}

对于 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,自动输出版本

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),
])
}