绘制图像时,我们要显示活动指示器。谁能帮助我们?
Answers:
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center=self.view.center;
[activityView startAnimating];
[self.view addSubview:activityView];
activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
适当的景观定向外观和旋转效果
代码更新为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)
您也可以UIActivityIndicatorView
在“界面生成器”中创建。只需将活动指示器视图拖动到情节提要上并设置属性即可。确保选中“停止时隐藏”。使用插座致电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.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview: activityIndicator];
[activityIndicator startAnimating];
[activityIndicator release];
如果支持旋转,请设置自动大小调整蒙版。干杯!!!
试试这个
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];
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),
])
}
如果要使用“自动布局”使微调器居中,请执行以下操作:
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]];
斯威夫特3,xcode 8.1
您可以使用小扩展名将UIActivityIndicatorView放置在UIView和继承的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()
在此示例中,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)
}
}
对于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];
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)
}
}
我找到了一种以最少的代码显示活动指标的方法:
首先导入您的UI Kit
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();
}