gpt4 book ai didi

ios - 带计时器的绘图应用程序。绘制后计时器开始滞后不到 20 秒

转载 作者:行者123 更新时间:2023-11-29 05:29:22 24 4
gpt4 key购买 nike

我在一些 friend 的帮助下构建了这个应用程序。我真的不知道代码是如何工作的。

基本上使用苹果铅笔记录数据(平板电脑上的时间、苹果铅笔的速度、笔划数等)。然而,随着时间的流逝和更多绘图的发生,计时器与实时时间不同步。

这个应用程序的目的是用于痴呆症研究,我让患者在平板电脑上画画,然后我收集相关信息。如果计时器发臭我就无法进行研究。

我尝试禁用所有计时器,但延迟仍然相同。我有一种感觉,这与笔画的采样方式有关。我只需要一个笔划数,不需要它显示每分钟的笔划数(这就是它当前正在做的)。我认为中风计数器可能是原因???

这是程序: https://drive.google.com/open?id=1lwzKwG7NLcX1qmE5yoxsdq5HICV2TNHm

class StrokeSegment {
var sampleBefore: StrokeSample?
var fromSample: StrokeSample!
var toSample: StrokeSample!
var sampleAfter: StrokeSample?
var fromSampleIndex: Int

var segmentUnitNormal: CGVector {
return segmentStrokeVector.normal!.normalized!
}

var fromSampleUnitNormal: CGVector {
return interpolatedNormalUnitVector(between: previousSegmentStrokeVector, and: segmentStrokeVector)
}

var toSampleUnitNormal: CGVector {
return interpolatedNormalUnitVector(between: segmentStrokeVector, and: nextSegmentStrokeVector)
}

var previousSegmentStrokeVector: CGVector {
if let sampleBefore = self.sampleBefore {
return fromSample.location - sampleBefore.location
} else {
return segmentStrokeVector
}
}

var segmentStrokeVector: CGVector {
return toSample.location - fromSample.location
}

var nextSegmentStrokeVector: CGVector {
if let sampleAfter = self.sampleAfter {
return sampleAfter.location - toSample.location
} else {
return segmentStrokeVector
}
}

init(sample: StrokeSample) {
self.sampleAfter = sample
self.fromSampleIndex = -2
}

@discardableResult
func advanceWithSample(incomingSample: StrokeSample?) -> Bool {
if let sampleAfter = self.sampleAfter {
self.sampleBefore = fromSample
self.fromSample = toSample
self.toSample = sampleAfter
self.sampleAfter = incomingSample
self.fromSampleIndex += 1
return true
}
return false
}
}

class StrokeSegmentIterator: IteratorProtocol {
private let stroke: Stroke
private var nextIndex: Int
private let sampleCount: Int
private let predictedSampleCount: Int
private var segment: StrokeSegment!

init(stroke: Stroke) {
self.stroke = stroke
nextIndex = 1
sampleCount = stroke.samples.count
predictedSampleCount = stroke.predictedSamples.count
if (predictedSampleCount + sampleCount) > 1 {
segment = StrokeSegment(sample: sampleAt(0)!)
segment.advanceWithSample(incomingSample: sampleAt(1))
}
}

func sampleAt(_ index: Int) -> StrokeSample? {
if index < sampleCount {
return stroke.samples[index]
}
let predictedIndex = index - sampleCount
if predictedIndex < predictedSampleCount {
return stroke.predictedSamples[predictedIndex]
} else {
return nil
}
}

func next() -> StrokeSegment? {
nextIndex += 1
if let segment = self.segment {
if segment.advanceWithSample(incomingSample: sampleAt(nextIndex)) {
return segment
}
}
return nil
}
}

例如,在真正的 25 秒处,应用程序显示的总时间为 20 秒。

最佳答案

A Timer不是用来计算耗时的东西。它是一种用于在一段时间过后触发执行的工具。但只是“在”一段时间过去之后,而不是“恰好在”一段时间过去之后。例如,做这样的事情:

var secondsElapsed: TimeInterval = 0.0
let timeInitiated = Date()

Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
secondsElapsed += 1
print("\(secondsElapsed) seconds should have passed but in reality \(Date().timeIntervalSince(timeInitiated)) second have passed")
}

您会发现两者并不相同,但非常接近。但是一旦我添加一些像这样的额外工作:

var 秒数:时间间隔 = 0.0让 timeInitiated = Date()

func countTo(_ end: Int) {
var string = ""
for i in 1...end {
string += String(i)
}
print("Just counted to string of lenght \(string.count)")
}

Timer.scheduledTimer(withTimeInterval: 1.0/60.0, repeats: true) { _ in
countTo(100000)
}

Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
secondsElapsed += 1
print("\(secondsElapsed) seconds should have passed but in reality \(Date().timeIntervalSince(timeInitiated)) second have passed")
}

我们遇到这样的情况:“14.0 秒应该已经过去,但实际上 19.17617702484131 秒已经过去了”。

我们让应用程序变得繁忙,因此它没有时间正确计数。

根据您的情况,您需要使用以下两种解决方案之一:

  1. 如果您对耗时感兴趣,只需使用 timeIntervalSince如第一个代码片段所示。
  2. 如果您需要确保每隔 N 触发一次秒你应该优化你的代码,考虑多线程......但最重要的是记住,你只能接近“每 N 秒”,不可能保证精确地每 N 执行一次秒。

关于ios - 带计时器的绘图应用程序。绘制后计时器开始滞后不到 20 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57801323/

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