我只想知道是否UIButton在特定时间间隔内被按下。(我的代码中为 2 秒)在 2 秒的时间段内,如果按下按钮,则某个值应为 1,如果未按下,则应为 2。
为了检查这一点,我在下面编写了代码。但unexpected结果即将到来,我不知道如何解决它。帮我!
import UIKit
class ViewController: UIViewController {
var gameTimer: Timer!
@IBAction func btnClick(_ sender: Any) {
if let button = sender as? UIButton {
if (button.isSelected) {
print(" Not Selected");
} else {
print(" Selected");
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
gameTimer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(btnClick(_:)), userInfo: nil, repeats: true)
} …Run Code Online (Sandbox Code Playgroud)