【问题标题】:UIActivity indicator or spinner in this case indicator is not showing up在这种情况下,UIActivity 指示器或微调器指示器未显示
【发布时间】:2018-07-14 18:08:43
【问题描述】:

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

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

}

【问题讨论】:

  • 永远不要在外部网站上上传代码。使用正确的代码格式在您的问题中包含Minimal, Complete and Verifiable example
  • @DávidPásztor 我如何编辑问题
  • 您可以将第一句巨大的句子拆分成更小的句子以增加可读性..(您的问题下方有一个编辑按钮)
  • 他说微调器没有显示
  • @MilanNosáľ 现在没有

标签: ios swift uiactivityindicatorview uiactivity


【解决方案1】:

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

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

这是使用自动布局的最佳时机,因为当您使用框架布局显示 spinner 时,pullUpView 框架已更改,这将导致不正确的渲染,使用自动布局将完美渲染

【讨论】:

  • 我复制并粘贴了你的代码它仍然没有出现
【解决方案2】:

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

【讨论】:

  • 这似乎仍然不起作用,请帮忙,现在代码有问题
  • 我试过了,但没用第一个答案是有效的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多