Позвольте мне сказать, что я не разработчик Swift/iOS.Доступ к ViewController из пользовательского делегата
Я создал обычай UIApplication
, чтобы поймать все события касания. Я хочу выполнить некоторые действия, когда экран остается нетронутым какое-то время.
Ниже мой код до сих пор:
import UIKit
@objc(MyApplication)
class MyApplication: UIApplication {
var screenUnTouchedTimer:Timer?
override func sendEvent(_ event: UIEvent) {
screenUnTouchedTimer?.invalidate()
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")
//screenUnTouchedTimer?.invalidate() // can be done as soon as this class is called, like above instead
}
super.sendEvent(event)
}
func makeScreenDim() {
UIScreen.main.brightness = CGFloat(0.15)
//print("makeScreenDim")
}
func makeScreenBright() {
UIScreen.main.brightness = CGFloat(0.5)
//print("makeScreenBright")
}
}
Приведенный выше код работает просто отлично, экран затемняется через 10 секунд, как ожидалось. Но я также хотел бы изменить некоторые элементы пользовательского интерфейса, показанные на моем ViewController, но я не знаю, как получить доступ к viewController от MyApplication
-class.
Я попытался следующие, я думаю, что должен быть способ сделать это:
var vc = ViewController()
vc.clearUserInterface()
Как я должен идти о вызвать функцию в ViewController?
Это вид в настоящее время viewController, к которому вы хотели бы иметь доступ? – dirtydanee
@dirtydanee Да, у меня есть только один ViewController. – theAlse