gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:10:41 27 4
gpt4 key购买 nike

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

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() 来设置微调器的动画,在这里你尝试将微调器定位在中间的 x,y拉 View

    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 显示 spinner 时,pullUpView 框架已更改,这将导致渲染不正确, 加上自动布局,它会完美呈现

关于ios - 在这种情况下,UIActivity 指示器或微调器未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48613199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com