实时数据画线时如何刷新屏幕
How to refresh the screen when I draw a line for realtime data
在我目前的swift程序中,一个函数在屏幕上用线条显示实时加速度数据。所以我用了classdrawRect
。结果每次刷新(关掉App再打开),加速度图都能画出来,但不是实时的。我知道我应该使用一些方法,例如 setNeedsDisplay()
来重绘它。但这没有用。也许我写错了。这是我的代码:
import UIKit
import CoreMotion
class draw: UIView, UIAccelerometerDelegate {
var motionManager = CMMotionManager()
override func drawRect(rect: CGRect) {
motionManager.deviceMotionUpdateInterval = 0.1
var acc_x: Double = 0.0
var temp_x: Double = 0.0
var i: CGFloat = 0
var accLine_x = UIGraphicsGetCurrentContext()
if(motionManager.deviceMotionAvailable) {
var queue = NSOperationQueue.mainQueue()
motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler: {
(deviceMotion: CMDeviceMotion!, error: NSError!) in
temp_x = acc_x
acc_x = deviceMotion.userAcceleration.x
CGContextSetLineWidth(accLine_x, 2)
CGContextSetStrokeColorWithColor(accLine_x, UIColor.redColor().CGColor)
CGContextMoveToPoint(accLine_x, i + 10, self.screenHeight * 436 + CGFloat(temp_x * 100))
CGContextAddLineToPoint(accLine_x, i + 13, self.screenHeight * 436 + CGFloat(acc_x * 100))
CGContextStrokePath(accLine_x)
i = (i + 3) % 320
})
}
}
}
调用 setNeedsDisplay 调用后调用了 drawRect 方法。 setNeedsDisplay 告诉系统您要更新绘图,在将来的某个时候,系统将调用 drawRect 方法(进行更新)。
因此,如果您有一个在基础数据更改时被调用的例程,那么发出 setNeedsDisplay 调用。
好的,这里有一些重写的代码...这里的重要点是您必须将绘图代码与更新隔离开来:
var acc_x : Double = 0.0
var temp_x : Double = 0.0
var i: CGFloat = 0
func startMonitoring() {
motionManager.deviceMotionUpdateInterval = 0.1
if(motionManager.deviceMotionAvailable) {
var queue = NSOperationQueue.mainQueue()
motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler: {
(deviceMotion: CMDeviceMotion!, error: NSError!) in
temp_x = acc_x
acc_x = deviceMotion.userAcceleration.x
// update things here
self.setNeedsDisplay()
})
}
}
override func drawRect(rect: CGRect) {
var accLine_x = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(accLine_x, 2)
CGContextSetStrokeColorWithColor(accLine_x, UIColor.redColor().CGColor)
CGContextMoveToPoint(accLine_x, i + 10, self.screenHeight * 436 + CGFloat(temp_x * 100))
CGContextAddLineToPoint(accLine_x, i + 13, self.screenHeight * 436 + CGFloat(acc_x * 100))
CGContextStrokePath(accLine_x)
i = (i + 3) % 320
}
请注意,我做的很快,但请确保你调用函数 "startMonitoring",它应该从加速度计获取更新,保存几个相关变量(你正在使用),然后调用设置需要显示。在某些时候,drawRect 将被调用并使用您保存的变量来正确绘制。
在我目前的swift程序中,一个函数在屏幕上用线条显示实时加速度数据。所以我用了classdrawRect
。结果每次刷新(关掉App再打开),加速度图都能画出来,但不是实时的。我知道我应该使用一些方法,例如 setNeedsDisplay()
来重绘它。但这没有用。也许我写错了。这是我的代码:
import UIKit
import CoreMotion
class draw: UIView, UIAccelerometerDelegate {
var motionManager = CMMotionManager()
override func drawRect(rect: CGRect) {
motionManager.deviceMotionUpdateInterval = 0.1
var acc_x: Double = 0.0
var temp_x: Double = 0.0
var i: CGFloat = 0
var accLine_x = UIGraphicsGetCurrentContext()
if(motionManager.deviceMotionAvailable) {
var queue = NSOperationQueue.mainQueue()
motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler: {
(deviceMotion: CMDeviceMotion!, error: NSError!) in
temp_x = acc_x
acc_x = deviceMotion.userAcceleration.x
CGContextSetLineWidth(accLine_x, 2)
CGContextSetStrokeColorWithColor(accLine_x, UIColor.redColor().CGColor)
CGContextMoveToPoint(accLine_x, i + 10, self.screenHeight * 436 + CGFloat(temp_x * 100))
CGContextAddLineToPoint(accLine_x, i + 13, self.screenHeight * 436 + CGFloat(acc_x * 100))
CGContextStrokePath(accLine_x)
i = (i + 3) % 320
})
}
}
}
调用 setNeedsDisplay 调用后调用了 drawRect 方法。 setNeedsDisplay 告诉系统您要更新绘图,在将来的某个时候,系统将调用 drawRect 方法(进行更新)。
因此,如果您有一个在基础数据更改时被调用的例程,那么发出 setNeedsDisplay 调用。
好的,这里有一些重写的代码...这里的重要点是您必须将绘图代码与更新隔离开来:
var acc_x : Double = 0.0
var temp_x : Double = 0.0
var i: CGFloat = 0
func startMonitoring() {
motionManager.deviceMotionUpdateInterval = 0.1
if(motionManager.deviceMotionAvailable) {
var queue = NSOperationQueue.mainQueue()
motionManager.startDeviceMotionUpdatesToQueue(queue, withHandler: {
(deviceMotion: CMDeviceMotion!, error: NSError!) in
temp_x = acc_x
acc_x = deviceMotion.userAcceleration.x
// update things here
self.setNeedsDisplay()
})
}
}
override func drawRect(rect: CGRect) {
var accLine_x = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(accLine_x, 2)
CGContextSetStrokeColorWithColor(accLine_x, UIColor.redColor().CGColor)
CGContextMoveToPoint(accLine_x, i + 10, self.screenHeight * 436 + CGFloat(temp_x * 100))
CGContextAddLineToPoint(accLine_x, i + 13, self.screenHeight * 436 + CGFloat(acc_x * 100))
CGContextStrokePath(accLine_x)
i = (i + 3) % 320
}
请注意,我做的很快,但请确保你调用函数 "startMonitoring",它应该从加速度计获取更新,保存几个相关变量(你正在使用),然后调用设置需要显示。在某些时候,drawRect 将被调用并使用您保存的变量来正确绘制。