【问题标题】:Dim the display when app not used for a while应用程序一段时间未使用时调暗显示
【发布时间】:2025-12-23 16:50:11
【问题描述】:

我想在我的应用程序运行时调暗手机屏幕,并且如果在特定时间段(例如 10 秒)内没有触摸事件,然后再次触摸屏幕上的任何位置时使屏幕变亮。

搜索 SO 后,似乎我需要创建一个自定义 UIApplication 以处理所有触摸。以下是我目前的代码:

import UIKit

@objc(MyApplication)

class MyApplication: UIApplication {

    override func sendEvent(_ event: UIEvent) {

        var screenUnTouchedTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.makeScreenDim), userInfo: nil, repeats: true);

        // Ignore .Motion and .RemoteControl event simply everything else then .Touches
        if event.type != .touches {
            super.sendEvent(event)
            return
        }

        // .Touches only
        var restartTimer = true
        if let touches = event.allTouches {
            // At least one touch in progress? Do not restart timer, just invalidate it
            self.makeScreenBright()
            for touch in touches.enumerated() {
                if touch.element.phase != .cancelled && touch.element.phase != .ended {
                    restartTimer = false
                    break
                }
            }
        }

        if restartTimer {
            // Touches ended || cancelled, restart timer
            print("Touches ended. Restart timer")
        } else {
            // Touches in progress - !ended, !cancelled, just invalidate it
            print("Touches in progress. Invalidate timer")
        }

        super.sendEvent(event)
    }

    func makeScreenDim() {
        UIScreen.main.brightness = CGFloat(0.1)
        print("makeScreenDim")
    }

    func makeScreenBright() {
        UIScreen.main.brightness = CGFloat(0.5)
        print("makeScreenBright")
    }
}

打印出来的样子是这样的:

makeScreenBright
Touches in progress. Invalidate timer
makeScreenBright
Touches ended. Restart timer
makeScreenDim
makeScreenDim
makeScreenDim
makeScreenDim
makeScreenDim
...

正如您在上面看到的,代码存在很大问题,似乎我正在为每个触摸事件创建一个新的计时器。我不知道如何在 UIApplication 中创建一个静态(只有一个)定时器。

我应该如何以正确的方式只实现一个计时器?

(我用的是Iphone7,最新版的swift和xcode)

【问题讨论】:

  • 将其创建为类属性,先使其无效,然后替换为新计时器
  • @Tj3n 谢谢,但我对 swift 很陌生,你能举个例子或编辑上面的代码
  • 当你需要结束定时器时,调用screenUnTouchedTimer.invalidate(),我在你的代码中看到你没有调用这个

标签: ios swift swift3


【解决方案1】:

您必须在某处使先前创建的计时器无效,否则您将获得所描述的行为。

在每次调用 sendEvent 时将其存储在一个属性中,以便下次调用该方法时可以访问它。

class MyApplication: UIApplication {

var screenUnTouchedTimer : Timer?

override func sendEvent(_ event: UIEvent) {

screenUnTouchedTimer?.invalidate()
screenUnTouchedTimer = Timer ......

【讨论】:

    最近更新 更多