通过Swift中的触摸检测应用程序何时处于活动状态或非活动状态

时间:2016-03-23 16:16:52

标签: ios swift performance optimization uiview

可以使用哪些代码来检测用户何时与应用交互以及用户何时未与应用进行交互?

目前,我有UIView func appActive(){ print("App received input from a touch, tap, swipe, long press etc.") } func appInactive(){ print("App stopped receiving any input.") } 通过覆盖触摸接收触摸,同时接收平移手势和点按手势。这个问题的解决方案需要与当前手势分开,或者高于这些手势。

是否有一个手势识别器位于其他所有可以告诉我的应用程序接收到手势时以及没有收到手势的情况下?

当应用处于有效状态时,是否有办法监控应用是否正在接收触摸以及何时无法根据需要调用功能,例如:

String contents = intent.getStringExtra("SCAN_RESULT");

                 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

谢谢。

2 个答案:

答案 0 :(得分:5)

  

Swift 2. Xcode 7.2。在iOS 7 - 9上测试。

     

改编自:    How to detect all touches in Swift 2       Subclass UIApplication with Swift

1 - 找到项目的Swift文件AppDelegate.swift,并注释掉@UIApplicationMain

//@UIApplicationMain

2 - 将新的Swift文件添加到名为main.swift的项目中,并添加代码:

import UIKit

UIApplicationMain(  
CommandLine.argc, UnsafeMutableRawPointer(CommandLine.unsafeArgv)  
    .bindMemory( to: UnsafeMutablePointer<Int8>.self,  
        capacity: Int(CommandLine.argc)), nil, NSStringFromClass(AppDelegate.self))  

3 - 将新的Swift文件添加到名为UIApplication.swift的项目中,并添加代码:

import UIKit

@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 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 timer
            print("Touches ended. Restart timer")
        } else {
            // Touches in progress - !ended, !cancelled, just invalidate it
            print("Touches in progress. Invalidate timer")
        }

        super.sendEvent(event)
    }
}

4 - 找到项目的Info.plist文件,然后添加新密钥(Xcode菜单:Editor > Add Item,选择或输入密钥Principal class,带字符串价值MyApplication

enter image description here

5 - 运行你的项目!

答案 1 :(得分:4)

拦截应用程序的任何触摸的方法是创建一个自定义UIWindow,它将捕获触摸而不取消它们。

class CustomWindow: UIWindow {

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        // Do any action you would like to perform to indicate the application is active
        return false
    }

}

您必须在Application Delegate中添加此窗口,并将其级别设置在主窗口上方。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var topWindow: CustomWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
        topWindow = CustomWindow(frame: UIScreen.mainScreen().bounds)
        topWindow?.rootViewController = UIViewController()
        topWindow?.windowLevel = UIWindowLevelNormal + 1
        topWindow?.hidden = false
        return true
    }