我有一个二级视图控制器,它使用一些触觉反馈。我正在触发预定定时器上的触觉反馈,每14秒循环一次。
Timer.scheduledTimer(withTimeInterval: 14, repeats: true) { _ in
self.changeLabel()
}这个计时器调用一个触发2次触觉反馈命中的函数。
@objc func changeLabel() {
if counter2 == 1 {
//Haptics
let impactGenerator = UIImpactFeedbackGenerator(style: .medium)
impactGenerator.prepare()
impactGenerator.impactOccurred()
Timer.scheduledTimer(withTimeInterval: 7, repeats: false) { _ in
let impactGenerator = UIImpactFeedbackGenerator(style: .medium)
impactGenerator.prepare()
impactGenerator.impactOccurred()}
//Changing Label
self.mainFocusLabel.text = self.foclabel1Text
self.manifestationImg.sd_setImage(with: URL(string: self.imglabel1URL))
self.counter2 = 2(有更多的代码,但它们几乎都与这个块相同,唯一的区别是,它只是将标签更改为2-10,并使用计数器来识别下一个要更改的标签。
我感到困惑的是--在视野被关闭后--振动(显然是计时器?)还在继续。我的手机每隔一段时间都在震动。我还没有看到任何禁用触觉反馈的代码。我可以想象,我可以将它添加到另一个视图控制器中来解决这个问题。
发布于 2021-06-13 00:39:49
解决了..。有点。
我必须让计时器成为一个变量
var forteenSecTimer : Timer?
forteenSecTimer = Timer.scheduledTimer(withTimeInterval: 14, repeats: true) { _ in
self.changeLabel()
}然后将此代码添加到退出按钮或已完成按钮中。我必须以编程方式替换“后退”按钮,以便在视图关闭时确保该代码被激活。
forteenSecTimer?.invalidate()我还必须将视图控制器表示风格从弹出更改为仅显示,因为我不确定如何激活该代码,因为视图只是向下滑落以摆脱它。
不是一个完美的解决方案,但这是我想出来的。
https://stackoverflow.com/questions/67932034
复制相似问题