通过Swift中的触摸检测应用程序何时处于活动状态或非活动状态 [英] Detecting when an app is active or inactive through touches in Swift

查看:209
本文介绍了通过Swift中的触摸检测应用程序何时处于活动状态或非活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



目前我有一个 ViewController 带有 UIView ,它通过覆盖触摸接收触摸,也可以通过接收平移手势和点击手势来接收触摸。这个问题的解决方案需要与那些当前的手势分开或者高于这些手势。



是否有一个手势识别器位于可以告诉我的应用程序的所有其他内容之上它收到手势并且没有收到手势?



当应用程序处于活动状态时,是否有办法监控应用是否正在接收触摸及何时接收不是,并根据需要调用函数,例如:

  func appActive(){
打印(应用程序从触摸,点击,滑动,长按等接收输入)
}

func appInactive(){
print(应用已停止接收任何输入。)
}

谢谢。

解决方案


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



改编自:






5 - 运行你的项目!





What code can be used to detect when a user is interacting with an app and when the user is not interacting with an app?

Currently I have a ViewController with a UIView that is receiving touches by overriding touches, also by receiving panning gestures and tap gestures. A solution to this problem needs to be separate to those current gestures or sit above those gestures.

Is there a gesture recognizer that sits above everything else that can tell my app when it received gestures and when there is no gesture received?

When the app is active, is there a way to monitor if the app is receiving touches and when it isn't and call a function as required, for example:

func appActive(){
    print("App received input from a touch, tap, swipe, long press etc.")
}

func appInactive(){
    print("App stopped receiving any input.")
}

Thank you.

解决方案

Swift 2. Xcode 7.2. Tested on iOS 7 - 9.

Adapted from: How to detect all touches in Swift 2 and Subclass UIApplication with Swift


1 - Locate your project's Swift file AppDelegate.swift, and comment out @UIApplicationMain:

//@UIApplicationMain


2 - Add a new Swift file to your project named exactly main.swift, and add the code:

import UIKit

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


3 - Add a new Swift file to your project named exactly UIApplication.swift, and add the code:

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 - Locate your project's Info.plist file, and add a new key (Xcode menu: Editor > Add Item), select or type key Principal class, with string value MyApplication.


5 - Run your project!


这篇关于通过Swift中的触摸检测应用程序何时处于活动状态或非活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆