gpt4 book ai didi

ios - 标签没有快速更新

转载 作者:行者123 更新时间:2023-11-28 11:19:55 24 4
gpt4 key购买 nike

我是 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 作为 block 执行,您必须更新主队列中的 UILabel,类似这样的方法可以解决这个问题,但仍然很乱

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 - 标签没有快速更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29222833/

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