iOS小技能:获取屏幕坐标的方式

「这是我参与2022首次更文挑战的第12天,活动详情查看:2022首次更文挑战」。

前言

获取屏幕坐标的方式:

  1. 获取touchDown(idx, x, y) 函数的坐标 的方法

x,y 整数型 屏幕坐标

屏幕坐标,横坐标为 x,纵坐标为 y,单位为像素。

例如,iPhone 4 与 iPhone 4S 的屏幕分辨率 为 640 * 960,则其最大横坐标为 640,最大纵坐标为 960。

lua脚本常常作为触动精灵的代码,而进行辅助完成功能。

  1. 使用iOS API获取在屏幕上的点击坐标
  2. 先截图,然后用photoshop打开,用PS 去描点获取。

I 使用catchTouchPoint 实现点坐标的获取


  • --获取用户点击的坐标

		--test
--获取用户点击的坐标 
         -- x,y = catchTouchPoint();

		-- sysLog("tcatchTouchPoint"..x..","..y);



		--end test
复制代码
  • 结果日志
 * PID 2751
Oct 15 10:45:27 iPhone iCal135c[2751] <Warning>: tcatchTouchPoint367,732
Oct 15 10:45:57 iPhone iCal135c[2751] <Warning>: tcatchTouchPoint269,1112
Oct 15 10:48:26 iPhone iCal135c[2751] <Warning>: tcatchTouchPoint365,769
Oct 15 10:48:35 iPhone iCal135c[2751] <Warning>: tcatchTouchPoint459,565
Oct 15 10:53:29 iPhone iCal135c[2751] <Warning>: tcatchTouchPoint405,722
exit

复制代码

II 使用iOS API获取在屏幕上的点击坐标

获取demo请关注公众号:iOS逆向

2.1 创建UIApplication 子类,实现sendEvent:获取在屏幕上的点击坐标

//获取在屏幕上的点击坐标

- (void)sendEvent:(UIEvent *)event{
    if (event.type==UIEventTypeTouches) {
        UITouch *touch = [event.allTouches anyObject];
        
        if (touch.phase == UITouchPhaseBegan) {
            self.isMoved = NO;
        }
        
//        if (touch.phase == UITouchPhaseMoved) {//滑动
//            self.isMoved = YES;
//        }
        
        if (touch.phase == UITouchPhaseEnded) {
            
            
            if (!self.isMoved && event.allTouches.count == 1) {//非多点触控,非滑动
                
                
                UITouch *touch = [event.allTouches anyObject];
                
                //在屏幕上的点击坐标
                CGPoint locationPointWindow = [touch preciseLocationInView:touch.window];//touch.view
                
                
                
                NSLog(@"TouchLocationWindow:(%.1f,%.1f)",locationPointWindow.x,locationPointWindow.y);
                
                
            }
            self.isMoved = NO;
        }
    }
    [super sendEvent:event];
}


复制代码

2.2 在main方法添加principalClassName

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    
    NSString * principalClassName;// The name of the UIApplication class or subclass. If you specify nil, UIApplication is assumed.


    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
        
        principalClassName =   NSStringFromClass([KNApplication4sendEvent class]);
        
        
        
        
        
        
    }
    

    return UIApplicationMain(argc, argv, principalClassName, appDelegateClassName);
    
    
}

复制代码

see also

更多内容请关注 #小程序:iOS逆向,只为你呈现有价值的信息,专注于移动端技术研究领域;更多服务和咨询请关注#公众号:iOS逆向

猜你喜欢

转载自juejin.im/post/7058490548549582884