Iphone 触摸移动和错误的屏幕范围?还是iOS中的Bug?

Iphone 触摸移动和错误的屏幕范围?还是iOS中的Bug?,iphone,objective-c,ios,cocoa-touch,uikit,Iphone,Objective C,Ios,Cocoa Touch,Uikit,我在iOS中发现了一个有趣的bug,但我试图相信我错了。你必须做两件事: 1为iOS创建单一视图模板 2在ViewController中编写小函数。m: - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* touch = [touches anyObject]; CGPoint point = [touch locationInView:[touch view]]; NS

我在iOS中发现了一个有趣的bug,但我试图相信我错了。你必须做两件事:

1为iOS创建单一视图模板

2在ViewController中编写小函数。m:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:[touch view]];
    NSLog(@"%@", NSStringFromCGPoint(point));
}// so u can detect points of your touch
所以,若你们试图在屏幕上移动手指,从屏幕的上到下纵向模式,你们会得到[-5.5..469]范围内的点数。。。我无法解释这一点,它只发生在设备上,在模拟器中工作正常

一些调试信息:


带状态栏且无WANTFullScreen布局范围为:[-25.5..449]


带状态栏和YES WantFullScreen布局范围为:[-5.5..469]


没有状态栏和否/是全屏布局,范围为:[-5.5..469]


带状态栏且无WANTFullScreenLayout view.frame为0、20、320、460,view.bounds为0、0、320、460


带状态栏和YES WantFullScreenLayout view.frame为0,0,320,480,view.bounds为0,0,320,480


没有状态栏和否/是FullScreenLayout view.frame为0、0、320、480,view.bounds为0、0、320、480


请帮我解释一下,它只发生在设备上…

因为状态栏超出了视图的范围。当你们触摸状态栏时,你们会得到负值

试试这个:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:[touch window]];
    NSLog(@"%@", NSStringFromCGPoint(point));
}

我从苹果的一个手指绘画程序教程中找到了这个:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect              bounds = [self bounds];
    UITouch*    touch = [[event touchesForView:self] anyObject];
    firstTouch = YES;
    // Convert touch point from UIView referential to OpenGL one (upside-down flip)
    location = [touch locationInView:self];
    location.y = bounds.size.height - location.y;
}

看起来您需要将触摸转换为OpenGL坐标以获得预期的结果。希望这能有所帮助。

根视图控制器的视图总是像纵向模式一样工作。您应该在根视图中插入一个新视图。根据苹果公司的说法,这个新的视图将正确运行,将给出正确的大小和坐标

比如,

UIView *v = self;
while ([[v superview] superview] != NULL) {
    v = [v superview];
}

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:v];

接触点将是正确的。

您是否考虑了状态栏?尝试在视图控制器上设置wantsFullScreenLayout并再次测试。有状态栏且没有wantsFullScreenLayout的范围是:[-25.5..449],有状态栏且是wantsFullScreenLayout的范围是:[-5.5..469],没有状态栏且没有/是FullScreenLayout的范围是:[-5.5..469],这很奇怪。请添加NSLog@%@,NSStringFromCGPointself.view;发布带有状态栏的输出,没有wantsFullScreenLayout范围是:[-25.5..449]带有状态栏,是wantsFullScreenLayout范围是:[-5.5..469]没有状态栏,没有/是FullScreenLayout范围是:[-5.5..469]@user1229413为什么你做出同样的评论?哦,对不起。我是stackoverflow的新手。我的意思是,我测试了它有和没有状态栏。所有范围在GLPaint中都是相同的错误,如果您在此处写入NSLog@%@,NSStringFromCGPointlocation,您将得到错误的范围-[-5,5..469]