如何在iPhone屏幕中间显示活动指标?
当我们绘制图像时,我们要显示活动指标。 任何人都可以帮忙吗?
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityView.center=self.view.center; [activityView startAnimating]; [self.view addSubview:activityView];
如果你正在使用Swift,那么你就是这么做的
let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge) activityView.center = self.view.center activityView.startAnimating() self.view.addSubview(activityView)
这是正确的方法:
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];
如果您支持旋转,请设置您的自动识别掩码。 干杯!!!
代码更新为Swift 3
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) } }
笔记
-
您可以将活动指标集中在屏幕中间,将其作为子视图添加到根
view
而不是myBlueSubview
。activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2) self.view.addSubview(activityIndicator)
-
您还可以在Interface Builder中创build
UIActivityIndicatorView
。 只需将一个活动指标视图拖动到故事板上并设置属性。 一定要检查时停止隐藏 。 使用sockets来调用startAnimating()
和stopAnimating()
。 看到这个教程 。
-
请记住,
LargeWhite
样式的默认颜色是白色。 所以,如果你有一个白色的背景,你不能看到它,只要将颜色改为黑色或其他任何你想要的颜色。activityIndicator.color = UIColor.black
-
一个常见的用例是在后台任务运行的时候。 这里是一个例子:
// 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() } }
试试这个方法
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];
你可以像这样设置位置
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; activityView.frame = CGRectMake(120, 230, 50, 50); [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,您可以使用以下内容:
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 }
希望这会起作用:
// 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];
如果你想尝试使用界面,你可以查看我的video 。 这样,你不需要写任何代码来显示在iPhone屏幕中间的活动指标。
Swift 3,xcode 8.1
你可以使用小的扩展名将UIActivityIndicatorView放在UIView的中心,并inheritanceUIView类:
码
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()
完整的例子
在这个例子中,UIActivityIndicatorView放置在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) } }
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) } }
我find了一种方法来显示最小代码的活动指标:
首先导入你的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(); }