iOS触摸坐标系

标签 ios

  • 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)sendEvent:(UIEvent *)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 是应用程序的主窗口。使用 myWindow = [[myWindow alloc] initWithFrame:[[UIScreen mainScreen] nativeBounds]];application: application didFinishLaunchingWithOptions: launchOptions
  3. 期间创建
  4. myWindow 没有 subview 。只有一个CAEAGLLayer

最佳答案

每个 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/

相关文章:

ios - 如何根据键对数组进行排序?

ios - GMSMarker 未在 1.3 中显示

ios - Firebase 身份验证 Twitter API 登录 "Nothing to see here"

ios - 如何在设置页面检查我的应用程序是否禁用了 Face ID

ios - 该应用程序在 payload/appname.app/appname : _terminateWithStatus 中引用了非公共(public)选择器

iphone - 将 Interface Builder 中的 UITextField 连接到代码中定义的 IBOutlet

ios - 将焦点设置到 tableCell 中的 UITextField

ios - 如何添加包含 iAd 横幅 View 的 subview ?

ios - 二元运算符 '+' 不能应用于类型 'CGRect' 和 'Double'

ios - 忽略 Xcode 6 中的特定警告