simbolo - ¿Cómo mostrar el indicador de actividad en medio de la pantalla del iphone?
porque aparece un circulo en mi iphone (14)
Cuando dibujamos la imagen, queremos mostrar el indicador de actividad. ¿Alguien puede ayudarnos?
Encontré una manera de mostrar el Indicador de actividad con un código mínimo:
en primer lugar, importe su kit de interfaz de usuario
import UIKit;
Luego debes iniciar el indicador de actividad
let activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView();
Luego solo copie y pegue estas funciones debajo de las cuales son auto explicativas y llámelos donde lo necesite:
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();
}
Espero que esto funcione:
// 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];
Esta es la forma correcta:
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];
Configure su máscara de aumento automático si admite la rotación. ¡¡¡Aclamaciones!!!
Para Swift 3 puede usar lo siguiente:
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
}
Prueba de esta manera
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];
Puede establecer la posición de esta manera
UIActivityIndicatorView *activityView =
[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(120, 230, 50, 50);
[self.view addSubview:activityView];
En consecuencia, cambie el tamaño del marco .....
Si desea agregar un texto junto con un indicador de actividad, consulte http://nullpointr.wordpress.com/2012/02/27/ios-dev-custom-activityindicatorview/
Si desea centrar la ruleta usando AutoLayout, haga lo siguiente:
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]];
Si desea intentar hacerlo utilizando la interfaz, puede ver mi video para esto . De esta forma, no es necesario escribir ningún código para mostrar el indicador de actividad en el medio de la pantalla del iPhone.
Si está usando Swift, así es como lo hace
let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
activityView.center = self.view.center
activityView.startAnimating()
self.view.addSubview(activityView)
Código actualizado para Swift 3
Código 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)
}
}
Notas
Puede centrar el indicador de actividad en el medio de la pantalla agregándolo como una subvista a la
view
raíz en lugar demyBlueSubview
.activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2) self.view.addSubview(activityIndicator)
También puede crear
UIActivityIndicatorView
en Interface Builder. Simplemente arrastre una Vista del Indicador de Actividad al guión gráfico y establezca las propiedades. Asegúrese de marcar Ocultar cuando se detuvo . Use una salida para llamar astartAnimating()
ystopAnimating()
. Ver este tutorial
Recuerde que el color predeterminado del estilo
LargeWhite
es blanco. Por lo tanto, si tiene un fondo blanco y no puede verlo, simplemente cambie el color a negro o cualquier otro color que desee.activityIndicator.color = UIColor.black
Un caso de uso común sería mientras se ejecuta una tarea en segundo plano. Aquí hay un ejemplo:
// 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() } }
Swift 3, xcode 8.1
Puede usar una extensión pequeña para colocar UIActivityIndicatorView en el centro de UIView y heredar las clases de UIView:
Código
extension UIActivityIndicatorView {
convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
self.init(activityIndicatorStyle: activityIndicatorStyle)
center = parentView.center
self.color = color
parentView.addSubview(self)
}
}
Cómo utilizar
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray, placeInTheCenterOf: view)
activityIndicator.startAnimating()
Ejemplo completo
En este ejemplo UIActivityIndicatorView colocado en el centro de la vista 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)
}
}
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center=self.view.center;
[activityView startAnimating];
[self.view addSubview:activityView];
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)
}
}