2017-05-14 19 views
0

私はswiftを使ってアプリを作る方法を学んでいて、あなたのスピードを伝える基本的なアプリを作りたかったのです。しかし、私は速度を更新する方法を理解できません。現時点では、私には初期の速度しか与えられず、レーベルを現在の速度で更新することはありません。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)" 
    } 


} 

答えて

0

使用デリゲートのメソッド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)" 
} 

使用すると、1つの呼び出し

+0

を削除することができますので、またあなたが、二回locationManager.startUpdatingLocation()これを呼び出しているあなたに感謝:ここではこれまでに私が持っているコードです!それはうまくいった! – TomEcho

関連する問題