标签未更新 swift
Label not updating swift
我是 iOS 的新手。我正在使用 MotionKit 从加速度计中检索数据。我可以在终端上看到加速度计的值每 0.1 秒被检索一次,不幸的是标签只更新一次。为什么标签没有更新?
@IBOutlet weak var xLabel: UILabel!
@IBOutlet weak var yLabel: UILabel!
@IBOutlet weak var zLabel: UILabel!
var xAccel = 0.0
var yAccel = 0.0
var zAccel = 0.0
private let queue = NSOperationQueue()
let motionKit = MotionKit()
override func viewDidLoad() {
super.viewDidLoad()
motionKit.getAccelerometerValues(interval: 0.1){
(x, y, z) in
println("x: \(x)")
println("y: \(y)")
println("z: \(z)")
println();
self.xAccel = x
self.yAccel = y
self.zAccel = z
self.xLabel.text = "\(self.xAccel)"
self.yLabel.text = "\(self.yAccel)"
self.zLabel.text = "\(self.zAccel)"
}
由于这个 MotionKit Framework 作为块执行,你必须更新主队列中的 UILabels,这样的事情会解决这个问题,但仍然很乱
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var xLabel: UILabel!
@IBOutlet weak var yLabel: UILabel!
@IBOutlet weak var zLabel: UILabel!
var xAccel = 0.0
var yAccel = 0.0
var zAccel = 0.0
private let queue = NSOperationQueue()
let motionKit = MotionKit()
override func viewDidLoad() {
super.viewDidLoad()
motionKit.getAccelerometerValues(interval: 0.1){
(x, y, z) in
println("x: \(x)")
println("y: \(y)")
println("z: \(z)")
println();
self.xAccel = x
self.yAccel = y
self.zAccel = z
dispatch_async(dispatch_get_main_queue(), {
self.xLabel.text = "\(self.xAccel)"
self.yLabel.text = "\(self.yAccel)"
self.zLabel.text = "\(self.zAccel)"
});
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
希望对您有所帮助
我是 iOS 的新手。我正在使用 MotionKit 从加速度计中检索数据。我可以在终端上看到加速度计的值每 0.1 秒被检索一次,不幸的是标签只更新一次。为什么标签没有更新?
@IBOutlet weak var xLabel: UILabel!
@IBOutlet weak var yLabel: UILabel!
@IBOutlet weak var zLabel: UILabel!
var xAccel = 0.0
var yAccel = 0.0
var zAccel = 0.0
private let queue = NSOperationQueue()
let motionKit = MotionKit()
override func viewDidLoad() {
super.viewDidLoad()
motionKit.getAccelerometerValues(interval: 0.1){
(x, y, z) in
println("x: \(x)")
println("y: \(y)")
println("z: \(z)")
println();
self.xAccel = x
self.yAccel = y
self.zAccel = z
self.xLabel.text = "\(self.xAccel)"
self.yLabel.text = "\(self.yAccel)"
self.zLabel.text = "\(self.zAccel)"
}
由于这个 MotionKit Framework 作为块执行,你必须更新主队列中的 UILabels,这样的事情会解决这个问题,但仍然很乱
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var xLabel: UILabel!
@IBOutlet weak var yLabel: UILabel!
@IBOutlet weak var zLabel: UILabel!
var xAccel = 0.0
var yAccel = 0.0
var zAccel = 0.0
private let queue = NSOperationQueue()
let motionKit = MotionKit()
override func viewDidLoad() {
super.viewDidLoad()
motionKit.getAccelerometerValues(interval: 0.1){
(x, y, z) in
println("x: \(x)")
println("y: \(y)")
println("z: \(z)")
println();
self.xAccel = x
self.yAccel = y
self.zAccel = z
dispatch_async(dispatch_get_main_queue(), {
self.xLabel.text = "\(self.xAccel)"
self.yLabel.text = "\(self.yAccel)"
self.zLabel.text = "\(self.zAccel)"
});
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
希望对您有所帮助