• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS 触控坐标系

[复制链接]
菜鸟教程小白 发表于 2022-12-13 14:53:31 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

  • locationInView: Returns the current location of the receiver in the coordinate system of the given view.

This method returns the current location of a UITouch object in the coordinate system of the specified view. Because the touch object might have been forwarded to a view from another view, this method performs any necessary conversion of the touch location to the coordinate system of the specified view.

    - (void)sendEventUIEvent *)event
    {
        if([event type] == UIEventTypeTouches)
        {
            NSSet<UITouch *> *allTouches = [event allTouches];
            if(allTouches)
            {
                for(UITouch* touch in allTouches)
                {
                    if([touch type] == UITouchTypeDirect)
                    {
                         // This is 640x1136
                         CGRect bounds = [myWindow bounds];

                         // This is in [318x560] empirically 
                         CGPoint origin = [touch locationInView:myWindow];
                    }
                }    
            }    
        }
    }

谁能解释一下?

来自问题:

  1. 在物理 iPhone 6 设备上进行测试。
  2. myWindow 是应用程序的主窗口。在 application: application didFinishLaunchingWithOptions: launchOptions
  3. 期间使用 myWindow = [[myWindow alloc] initWithFrame:[[UIScreen mainScreen] nativeBounds]]; 创建
  4. myWindow 没有 subview 。只有一个CAEAGLLayer



Best Answer-推荐答案


每个 View 都有一个 contentScaleFactor 属性。它旨在简化使用不同设备分辨率的工作(例如视网膜 (@2x)、非视网膜 (@1x)、@3x 等) UIWindowUIView 的子类,所以它也有它。

The scale factor determines how content in the view is mapped from the logical coordinate space (measured in points) to the device coordinate space (measured in pixels). This value is typically either 1.0 or 2.0. Higher scale factors indicate that each point in the view is represented by more than one pixel in the underlying layer. For example, if the scale factor is 2.0 and the view frame size is 50 x 50 points, the size of the bitmap used to present that content is 100 x 100 pixels.

The default value for this property is the scale factor associated with the screen currently displaying the view. If your custom view implements a custom drawRect: method and is associated with a window, or if you use the GLKView class to draw OpenGL ES content, your view draws at the full resolution of the screen. For system views, the value of this property may be 1.0 even on high resolution screens.

因此,在您的情况下,[myWindow bounds] 返回等于设备屏幕大小的逻辑大小,因为您将其设置为 native 边界。您的窗口在设备坐标中比屏幕大两倍,设备坐标是根据其比例因子(等于 2)根据其逻辑坐标计算得出的,并且您的触摸仅命中它的第四部分。

myWindow = [[myWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myWindow.layer.contentsScale = [[UIScreen mainScreen] nativeScale];

CGPoint origin = [touch locationInView:myWindow];
origin.x *= [[UIScreen mainScreen] scale];
origin.y *= [[UIScreen mainScreen] scale];               

关于iOS 触控坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35591681/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap