如何检测 Swift 中的所有触摸 2

How to detect all touches in Swift 2

我正在尝试为我正在使用 Swift 2 开发的应用程序创建一个超时函数,但在 swift 2 中,您可以将此代码放在应用程序委托中并且它可以工作但是它不检测任何键盘按下、按钮按下、文本字段按下等:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesBegan(touches, withEvent: event);
    let allTouches = event!.allTouches();

    if(allTouches?.count > 0) {
        let phase = (allTouches!.first as UITouch!).phase;
        if(phase == UITouchPhase.Began || phase == UITouchPhase.Ended) {
            //Stuff
            timeoutModel.actionPerformed();
        }
    }
}

在 swift 2 之前,我能够拥有 AppDelegate 子类 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)
            [[InactivityModel instance] actionPerformed];
    }
}

上面的代码适用于每次触摸,但 swift 等效代码仅在 UIWindow 层次结构之上不存在视图时才有效?

有谁知道检测应用程序中每次触摸的方法吗?

因为我的应用程序中有类似的东西,我只是试图修复它:

  • 覆盖 UIWindow 中的 sendEvent - 不起作用
  • 在委托中覆盖 sendEvent - 不起作用

所以唯一的办法就是提供自定义UIApplication subclass。到目前为止我的代码(适用于 iOS 9)是:

@objc(MyApplication) class MyApplication: UIApplication {

  override func sendEvent(event: UIEvent) {
    //
    // 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 auto lock timer, just invalidate it
      //
      for touch in touches.enumerate() {
        if touch.element.phase != .Cancelled && touch.element.phase != .Ended {
          restartTimer = false
          break
        }
      }
    }

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

    super.sendEvent(event)
  }

}

为什么有 @objc(MyApplication)。那是因为 Swift 以不同于 Objective-C 的方式破坏名称,它只是说 - 我的 class 在 Objective-C 中的名称是 MyApplication.

要使其正常工作,请打开您的 info.plist 并添加包含 Principal class 键和 MyApplication 值的行(MyApplication@objc(...) 里面的东西,不是你的 Swift class 名字)。原始密钥是 NSPrincipalClass.

UIWindow 还有一个可以覆盖的 sendEvent 方法。这将允许您跟踪自上次屏幕触摸以来的时间。 Swift 4:

class IdlingWindow: UIWindow {
    /// Tracks the last time this window was interacted with
    var lastInteraction = Date.distantPast

    override func sendEvent(_ event: UIEvent) {
        super.sendEvent(event)
        lastInteraction = Date()
    }
}

如果您使用的是情节提要,可以在 didFinishLaunchingWithOptions:

中加载它
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: IdlingWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = IdlingWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateInitialViewController()
        window?.makeKeyAndVisible()
        return true
    }
    :
}

extension UIApplication {
    /// Conveniently gets the last interaction time
    var lastInteraction: Date {
        return (keyWindow as? IdlingWindow)?.lastInteraction ?? .distantPast
    }
}

现在,在您应用的其他地方,您可以像这样检查不活动:

if UIApplication.shared.lastInteraction.timeIntervalSinceNow < -2 {
    // the window has been idle over 2 seconds
}

@markiv 的回答很有魅力,有两个问题:

  1. keyWindow
    • 'keyWindow' 在 iOS 13.0 中被弃用:不应该用于支持多个场景的应用程序,因为它 returns 一个键 window在所有连接的场景中

可以这样解决:

let kw = UIApplication.shared.windows.filter {[=11=].isKeyWindow}.first 找到

查看@matt 的回答

  1. AppDelegate - 我收到此消息:

    • 应用委托必须实现 window 属性 如果它想使用主故事板文件

一个可以子class UIWindow - 找到答案here。将它与 IdlingWindow class 结合起来。

消息消失了。

var customWindow: IdlingWindow?    
var window: UIWindow? {
    get {
        customWindow = customWindow ?? IdlingWindow(frame: UIScreen.mainScreen().bounds)
        return customWindow
    }
    set { }
}