是否有人实现了一个功能,如果用户在某个时间段内没有touch 屏幕,您会采取某个操作?我在想最好的办法.

在UIApplication中有这个有点相关的方法:

[UIApplication sharedApplication].idleTimerDisabled;

如果你有这样的东西就好了:

NSTimeInterval timeElapsed = [UIApplication sharedApplication].idleTimeElapsed;

然后我可以设置一个计时器,定期判断这个值,并在它超过阈值时采取一些措施.

希望这能解释我要找的东西.有没有人已经解决了这个问题,或者有什么 idea 你会怎么做?谢谢.

推荐答案

这就是我一直在寻找的答案:

让应用程序委托子类UIApplication.在实现文件中,重写sendEvent:方法,如下所示:

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];

    // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }
}

- (void)resetIdleTimer {
    if (idleTimer) {
        [idleTimer invalidate];
        [idleTimer release];
    }

    idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}

- (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");
}

其中maxIdleTime和idleTimer是实例变量.

为了使其正常工作,您还需要修改main.m以告诉UIApplicationMain使用您的委托类(在本例中为AppDelegate)作为主体类:

int retVal = UIApplicationMain(argc, argv, @"AppDelegate", @"AppDelegate");

Ios相关问答推荐

为什么Reaction-Native-Vision-Camera不能与闪光灯一起使用?

如何让3张组合的`SF Symbol`图片围绕一个特定点旋转?

SwiftData在获取值时应用程序崩溃:线程1:EXC_BREAKPOINT(代码=1,子代码=0x101e8303c)

当操作修改查看条件时,按钮不重复

iOS 17-在Xcode 15中添加导航控制器时,导航栏不会立即显示

Toast 不显示 Flutter

如何擦除 Swift 中的 Core Graphics Context 绘图?

将角半径仅应用于 Swiftui 中按钮的两个角

迁移到 UIKit 生命周期的应用程序不会调用 SceneDelegate

为什么我的应用在 Testflight 中没有下载 dSYM 按钮?

UIScrollview 获取touch 事件

如何在 UIActivityViewController 中设置邮件主题?

如何在导航栏右侧添加多个 UIBarButtonItem?

UIView 和 initWithFrame 以及一个 NIB 文件.如何加载 NIB 文件?

iPhone 未连接.连接 iPhone 后 Xcode 将继续

正确的领域使用模式/最佳实践?

是否可以将 Socket.io 与 AWS Lambda 一起使用?

在 NSUserDefaults 中存储值是否有任何限制?

Info.plist 实用程序错误:无法打开 Info.plist,因为没有这样的文件

Xcode中的终端窗口?