计时器进入 negatives/not 重置
Timer going into negatives/not reseting
我正在尝试为我的应用程序设置倒数计时器。我的代码有效,因此每次拖动图像 whiteDot 并包含 smallDot 时,倒计时开始,并且 smallDot 会在屏幕上的随机位置生成。我有几个问题
1.) 我试图在每次执行 "if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil)" 语句后将计时器重置为 2 秒。
2.) "if" 语句每执行一次,正常倒计时,但在倒计时归零之前再次执行时,它开始变成负数并且计数快于 1 秒。
import UIKit
var timeClock = 2
class SecondViewController: UIViewController {
func startTimer() {
timeClock -= 1
time.text = "Time: " + String(timeClock)
if whiteDot.frame.contains(smallDot.frame) && timeClock > 0 {
timeClock = 2
}
else if timeClock == 0 || timeClock < 0 {
timer.invalidate()
}
}
var timer = Timer()
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
addOne += 1
score.text = "\(addOne)"
smallDot.center = spawnRandomPosition()
timeClock = 2
if timeClock == 0 || timeClock < 0 {
timer.invalidate()
}
else if timeClock > 0 && (whiteDot.frame.contains(smallDot.frame)){
timeClock = 2
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.action), userInfo: nil, repeats: true)
}
}
var timeClock = 2
由于这出现在 class 中但在任何方法之外,因此此行为您的 class 声明了一个 属性 称为 timeClock
并将其初始化为 2
.对于您创建的 class 的任何实例,这将是 timeClock
属性 的起始值。
你在倒计时时正确地更新了这个值,但是你没有在任何地方将它重置为 2
。将 属性 声明置于 action()
方法下方并不意味着它会在之后执行。 属性 每个实例只初始化一次。
您要做的是在安排计时器之前将 timeClock
的值设置为 2
,或者在使计时器无效后将其设置为 2
。无论哪种方式,您都需要将其重置为 2
某处。
刚发现我的问题。我根本没有重置计时器。首先,我试图在 'action' 函数中重置它,但我只需要将它向下移动到 if 语句的底部即可解决我所有的问题。此代码适用于在发生特定操作时尝试重置计时器的任何人。
class SecondViewController: UIViewController
{
var addOne = 0
func startTimer() {
timeClock -= 1
time.text = "Time: " + String(timeClock)
if timeClock <= 0 {
timer.invalidate()
//show game over
}
}
func resetTimer() {
timer.invalidate()
timeClock = 3
}
var timer = Timer()
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
addOne += 1
score.text = "\(addOne)"
resetTimer()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.startTimer), userInfo: nil, repeats: true)
smallDot.center = spawnRandomPosition()
}
}
我正在尝试为我的应用程序设置倒数计时器。我的代码有效,因此每次拖动图像 whiteDot 并包含 smallDot 时,倒计时开始,并且 smallDot 会在屏幕上的随机位置生成。我有几个问题
1.) 我试图在每次执行 "if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil)" 语句后将计时器重置为 2 秒。
2.) "if" 语句每执行一次,正常倒计时,但在倒计时归零之前再次执行时,它开始变成负数并且计数快于 1 秒。
import UIKit
var timeClock = 2
class SecondViewController: UIViewController {
func startTimer() {
timeClock -= 1
time.text = "Time: " + String(timeClock)
if whiteDot.frame.contains(smallDot.frame) && timeClock > 0 {
timeClock = 2
}
else if timeClock == 0 || timeClock < 0 {
timer.invalidate()
}
}
var timer = Timer()
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
addOne += 1
score.text = "\(addOne)"
smallDot.center = spawnRandomPosition()
timeClock = 2
if timeClock == 0 || timeClock < 0 {
timer.invalidate()
}
else if timeClock > 0 && (whiteDot.frame.contains(smallDot.frame)){
timeClock = 2
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.action), userInfo: nil, repeats: true)
}
}
var timeClock = 2
由于这出现在 class 中但在任何方法之外,因此此行为您的 class 声明了一个 属性 称为 timeClock
并将其初始化为 2
.对于您创建的 class 的任何实例,这将是 timeClock
属性 的起始值。
你在倒计时时正确地更新了这个值,但是你没有在任何地方将它重置为 2
。将 属性 声明置于 action()
方法下方并不意味着它会在之后执行。 属性 每个实例只初始化一次。
您要做的是在安排计时器之前将 timeClock
的值设置为 2
,或者在使计时器无效后将其设置为 2
。无论哪种方式,您都需要将其重置为 2
某处。
刚发现我的问题。我根本没有重置计时器。首先,我试图在 'action' 函数中重置它,但我只需要将它向下移动到 if 语句的底部即可解决我所有的问题。此代码适用于在发生特定操作时尝试重置计时器的任何人。
class SecondViewController: UIViewController
{
var addOne = 0
func startTimer() {
timeClock -= 1
time.text = "Time: " + String(timeClock)
if timeClock <= 0 {
timer.invalidate()
//show game over
}
}
func resetTimer() {
timer.invalidate()
timeClock = 3
}
var timer = Timer()
@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.view)
if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
addOne += 1
score.text = "\(addOne)"
resetTimer()
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.startTimer), userInfo: nil, repeats: true)
smallDot.center = spawnRandomPosition()
}
}