use*_*509 5 optimization performance uiview ios swift
可以使用哪些代码来检测用户何时与应用程序交互以及用户何时未与应用程序交互?
目前,我有一个ViewController具有UIView由覆盖触摸接收触摸,也可接收平移手势和轻敲姿势.此问题的解决方案需要与当前手势分开或者位于这些手势之上.
是否有一个手势识别器位于其他所有可以告诉我的应用程序接收到手势和没有收到手势时?
当应用程序处于活动状态时,是否有办法监控应用程序是否正在接收触摸,何时不是,并根据需要调用函数,例如:
func appActive(){
print("App received input from a touch, tap, swipe, long press etc.")
}
func appInactive(){
print("App stopped receiving any input.")
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
拦截应用程序任何触摸的一种方法是创建一个自定义UIWindow,该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
}
}
Run Code Online (Sandbox Code Playgroud)
您必须将此窗口添加到“应用程序代表”中,并将其级别设置为主窗口之上。
@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
}
Run Code Online (Sandbox Code Playgroud)
Swift 2. Xcode 7.2.在iOS 7 - 9上测试过.
1 - 找到项目的Swift文件AppDelegate.swift,并注释掉@UIApplicationMain:
//@UIApplicationMain
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
4 -找到你的项目的Info.plist文件,并添加一个新的密钥(Xcode的菜单:Editor > Add Item),选择或输入键Principal class,用字符串值MyApplication.
5 - 运行你的项目!
| 归档时间: |
|
| 查看次数: |
6911 次 |
| 最近记录: |