在这种情况下,UIActivity 指示器或微调器未显示

UIActivity indicator or spinner in this case indicator is not showing up

所以,我写了一个程序 那是一个地图应用程序,允许用户查看那里的位置 当他们打开应用程序并双击时,它允许他们放下图钉 隐藏的底部视图显示出来,我想在该视图中显示图像,因为我将视图的高度设置得非常小,我不能使用故事板在其中添加组件,我添加了一个 activity 指示器
带有代码,但由于某种原因 activity 指示器未显示在屏幕上 即使隐藏视图的高度发生变化并且出现。这是代码

import UIKit
import MapKit
import CoreLocation
class MapVC: UIViewController, UIGestureRecognizerDelegate {


@IBOutlet weak var pullUpViewHighConstraint: NSLayoutConstraint!
@IBOutlet weak var pullUpView: UIView!
@IBOutlet weak var mapView: MKMapView!


var locationManager = CLLocationManager()
var authStatus = CLLocationManager.authorizationStatus()
let regiounRadius: Double = 1000
var spinner: UIActivityIndicatorView?
var progrssLabel: UILabel?
var screen = UIScreen.main.bounds



override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.mapView.showsUserLocation = true
    configureLocationServices()
    addDoubleTap()
}


func addDoubleTap() {
    let doubleTap = UITapGestureRecognizer(target: self, action:  #selector(droupPin(sender:)))
    doubleTap.numberOfTapsRequired = 2
    doubleTap.delegate = self

    mapView.addGestureRecognizer(doubleTap)
}

func addSwipe() {
    let swipe = UISwipeGestureRecognizer(target: self, action:  #selector(animateViewDown))
    swipe.direction = .down
    pullUpView.addGestureRecognizer(swipe)
}

func animateViewUp() {
    pullUpViewHighConstraint.constant = 300
    UIView.animate(withDuration: 0.4) {
        self.view.layoutIfNeeded()
    }

}

@objc func animateViewDown() {
    pullUpViewHighConstraint.constant = 0
    UIView.animate(withDuration: 0.4) {
        self.view.layoutIfNeeded()
    }
}

func addSpinner() {
    spinner = UIActivityIndicatorView()
    spinner?.center = CGPoint(x: screen.width / 2 -  (spinner?.frame.width)! / 2, y: 150)
    spinner?.activityIndicatorViewStyle = .whiteLarge
    spinner?.color = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
    spinner?.stopAnimating()
    pullUpView.addSubview(spinner!)
}


@IBAction func centerMapBtnPressed(_ sender: Any) {
    if authStatus == .authorizedAlways || authStatus ==  .authorizedWhenInUse{
        centerMapOnUserLocation()
        // removePin()

    }else {return}
}

}

 extension MapVC: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "droppablePin")
    pinAnnotation.pinTintColor = #colorLiteral(red: 0.9771530032, green: 0.7062081099, blue: 0.1748393774, alpha: 1)
    pinAnnotation.animatesDrop = true
    return pinAnnotation
}

func centerMapOnUserLocation() {
    guard let coordinets = locationManager.location?.coordinate else    { return }
    let coordinateRegion =    MKCoordinateRegionMakeWithDistance(coordinets, regiounRadius*2.0,  regiounRadius*2.0)
    mapView.setRegion(coordinateRegion, animated: true)
}

@objc func droupPin(sender: UITapGestureRecognizer) {
    removePin()
    animateViewUp()
    addSwipe()
    addSpinner()
    let touchPoint = sender.location(in: mapView)
    let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    let annotation = DroppablePin(coordinatee: touchCoordinate, identifierr: "droppablePin")
    mapView.addAnnotation(annotation)

    let coordinateRegion =  MKCoordinateRegionMakeWithDistance(touchCoordinate, regiounRadius*2.0, regiounRadius*2.0)
    mapView.setRegion(coordinateRegion, animated: true)

}

func removePin() {
    for annotation in mapView.annotations {
        mapView.removeAnnotation(annotation)
    }
}

}

extension MapVC: CLLocationManagerDelegate {

func configureLocationServices() {
    if authStatus == .notDetermined {
        locationManager.requestAlwaysAuthorization()
    }
    else {
        return
    }
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    centerMapOnUserLocation()
}

}

首先微调器没有显示是因为你没有正确定位它,其次你应该使用 startAnimating() 来设置微调器的动画,在这里你尝试将微调器定位在 pullView[ 的中间 x,y =14=]

    spinner = UIActivityIndicatorView() 
    spinner.translatesAutoresizingMaskIntoConstraints = false     
    spinner?.activityIndicatorViewStyle = .whiteLarge
    spinner?.color =  colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
    pullUpView.addSubview(spinner!)
   // center in container
    spinner.centerXAnchor.constraint(equalTo: pullUpView.centerXAnchor).isActive = true
    spinner.centerYAnchor.constraint(equalTo: pullUpView.centerYAnchor).isActive = true
    spinner?.startAnimating()

这是使用自动布局的最佳时机,因为当您使用 frame-layout 显示 spinnerpullUpView 框架已更改,这将导致渲染不正确,请使用 auto -layout 它将完美呈现

spinner?.stopAnimating() 替换为 spinner?.startAnimating(),您不希望它停止但 运行。