Android 和iOS 获取屏幕点击位置

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

参考
参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值