저는 swift를 사용하여 앱을 만드는 법을 배우고 있으며 속도를 알려주는 기본 앱을 만들고 싶었습니다. 그러나 속도를 업데이트하는 방법을 알 수 없습니다. 현재로서는 초기 속도 만 제공하고 레이블을 현재 속도로 업데이트하지 않습니다. 다음은 내가해야 할 코드입니다.
@IBOutlet var speedLabel: UILabel!
@IBOutlet var countLabel: UILabel!
let locationManager = CLLocationManager()
var speed: CLLocationSpeed = CLLocationSpeed()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.startUpdatingLocation()
speed = locationManager.location!.speed
if speed < 0 {
speedLabel.text = "No movement registered"
}
else {
speedLabel.text = "\(speed)"
}
}
대리인의 방법 https://developer.apple.com/reference/corelocation/cllocationmanagerdelegate 사용
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
guard let speed = manager.location?.speed else { return }
speedLabel.text = speed < 0 ? "No movement registered" : "\(speed)"
}
또한 이것을 두 번 호출 locationManager.startUpdatingLocation()
하고 있으므로 하나의 호출을 제거 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다