我想在“x”分钟后在我的应用程序中设置自动超时场景。我尝试使用计划定时器调用定时器(“x”分钟后)。用户正在阅读具有可滚动/点击状态的 T&C 页面,同时触发了计时器。但是当用户不执行任何操作(触摸、滚动)时,我期待计时器调用。简单地说,如果用户在指定的时间段内没有触摸屏幕,我需要调用超时弹出窗口。我想为我的整个应用程序处理这种超时情况。
最佳答案
您可以在接收屏幕触摸的 UIWindow
上使用 UITapGestureRecognizer
。
在这里,我添加了 5 秒的计时器,超时函数将调用。
在Appdelegate
中添加UIGestureRecognizerDelegate
var window: UIWindow?
var timer: Timer?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tapGesture = UITapGestureRecognizer(target: self, action: nil)
tapGesture.delegate = self
window?.addGestureRecognizer(tapGesture)
//Start timer for first time
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.timeOut), userInfo: nil, repeats: false)
return true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
print(" tapped on screen,")
timer?.invalidate()
//set Timer Ex: 5 Second
//Run timer on touch
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.update), userInfo: nil, repeats: false)
return false
}
@objc func timeOut() {
print("TimeOut")
}
关于ios - 如何知道您的应用程序是事件的还是空闲的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47278193/