gpt4 book ai didi

ios - 用户不活动,使用 AppDelegate Swift 4 显示屏幕保护程序

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

我需要通过检测所有触摸来检测所有 View Controller 上的用户不活动状态。我当前的 AppDelegate 代码未检测到 UIButton 和其他 UI 控件上的触摸。如何检测所有触摸操作,包括 UIButtonsUILabelsUITextfields?我看过很多 Stack Overflow 文章,但我似乎无法使其适应我的需要。如果这是不可能的,我如何将原始的 TimerUIApplication 类扩展到其他 View 以检测所做的触摸。提前致谢。

AppDegegate代码:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
var window: UIWindow?
static let ApplicationDidTimoutNotification = "AppTimout"

// The timeout in seconds for when to fire the idle timer.
let timeoutInSeconds: TimeInterval = 5

var idleTimer: Timer?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
application.isIdleTimerDisabled = true

let userDefaults = UserDefaults.standard
let defaultValues = ["promotionIsEnabled_preference" : "YES",
"promotionDuration_preference" : "10"]
userDefaults.register(defaults: defaultValues)
userDefaults.synchronize()
return true
}



func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
print ("app started")
self.resetIdleTimer()
idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds, target: self, selector: #selector(AppDelegate.idleTimerExceeded), userInfo: nil, repeats: false)

}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

print("touched")
self.resetIdleTimer()

}

// Resent the timer because there was user interaction.
func resetIdleTimer() {
if let idleTimer = idleTimer {
idleTimer.invalidate()
}
}


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.resetIdleTimer()
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds, target: self, selector: #selector(AppDelegate.idleTimerExceeded), userInfo: nil, repeats: false)

}

@objc func idleTimerExceeded() {

NotificationCenter.default.post(name: Notification.Name(rawValue: TimerUIApplication.ApplicationDidTimoutNotification), object: nil)
print ("Inactive User")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "mainPromo") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()

}


}

我在 AppDelegate 中使用的原始代码。

import UIKit
import Foundation

class TimerUIApplication: UIApplication {
static let ApplicationDidTimoutNotification = "AppTimout"

// The timeout in seconds for when to fire the idle timer.
let timeoutInSeconds: TimeInterval = 5

var idleTimer: Timer?

// Resent the timer because there was user interaction.
func resetIdleTimer() {
if let idleTimer = idleTimer {
idleTimer.invalidate()
}

idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds, target: self, selector: #selector(TimerUIApplication.idleTimerExceeded), userInfo: nil, repeats: false)
}

// If the timer reaches the limit as defined in timeoutInSeconds, post this notification.
@objc func idleTimerExceeded() {

NotificationCenter.default.post(name: Notification.Name(rawValue: TimerUIApplication.ApplicationDidTimoutNotification), object: nil)
print ("out")
}


override func sendEvent(_ event: UIEvent) {

super.sendEvent(event)

if idleTimer != nil {
self.resetIdleTimer()
}

if let touches = event.allTouches {
for touch in touches {
if touch.phase == UITouchPhase.began {
self.resetIdleTimer()
}
}
}

}
}

最佳答案

Swift 4 - 在 AppDelegate.swift 中创建一个 CustomWindow,它会覆盖您的 Storyboard View ,注册触摸,但不会取消它们。代码:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{

var topWindow: CustomWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
topWindow = CustomWindow(frame: UIScreen.main.bounds)
topWindow?.rootViewController = UIViewController()
topWindow?.windowLevel = UIWindowLevelNormal + 1
topWindow?.isHidden = false
}

创建一个 CustomWindow.swift 类来接收并处理您的操作。代码:

import UIKit

class CustomWindow: UIWindow{

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
// What you want to do in here.
return false
}

关于ios - 用户不活动,使用 AppDelegate Swift 4 显示屏幕保护程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54904455/

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