Android需要在整个App中获取点击屏幕的点坐标,方便统计用户使用App的情况。
找了许多方法,,需要自己创建一个baseactivity,然后继承方法,
/**
* Called to process touch screen events. You can override this to
* intercept all touch screen events before they are dispatched to the
* window. Be sure to call this implementation for touch screen events
* that should be handled normally.
*
* @param ev The touch screen event.
*
* @return boolean Return true if this event was consumed.
*/
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
iOS swift,由于swift里面没有main.swift
文件,需要创建这个文件,去掉AppDelegate
里面的@UIApplicationMain
//@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
main.swift
import UIKit
import Foundation
UIApplicationMain(CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)),
NSStringFromClass(CustomApplication.self),
NSStringFromClass(AppDelegate.self)
)
CustomApplication.swift
import Foundation
import UIKit
// swiftlint:disable force_cast
class CustomApplication: UIApplication {
//uiapplicton中有与Event相关的api,所以可以利用这些api完成记录行为日志的任务
override func sendEvent(_ event: UIEvent) {
//在这里处理一些统一的逻辑
print("来自UIApplication 的 event")
if event.type == .touches{
let touch: UITouch = event.allTouches!.first!
if touch.phase == .began {
let now = Date()
let timeInterval: TimeInterval = now.timeIntervalSince1970
let locationPointWindow: CGPoint = touch .preciseLocation(in: touch.window)
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.touchEventRecord?.type = "touchevent"
appDelegate.touchEventRecord?.positionX = Float(locationPointWindow.x)
appDelegate.touchEventRecord?.positionY = Float(locationPointWindow.y)
appDelegate.touchEventRecord?.time = Int(timeInterval*1000)
}
}
return super .sendEvent(event)
}
}