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)
}
}