【问题标题】:TouchMoved and wrong screen range? or Bug in iOS?TouchMoved 和错误的屏幕范围?还是 iOS 中的错误?
【发布时间】:2012-11-04 19:26:00
【问题描述】:

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

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] 范围内的点...我无法解释这一点,它只发生在设备上,在模拟器中可以正常工作。

一些调试信息:

带状态栏和 NO WantedFullScreenLayout 范围是:[-25.5 .. 449]

带状态栏和YES WantsFullScreenLayout 范围是:[-5.5 .. 469]

没有状态栏和 NO/YES FullScreenLayout 范围是:[-5.5 .. 469]


带状态栏和 NO WantsFullScreenLayout view.frame 是 (0, 20, 320, 460) 并且 view.bounds 是 (0, 0, 320, 460)

带有状态栏和YES WantsFullScreenLayout view.frame 是 (0, 0, 320, 480) 并且 view.bounds 是 (0, 0, 320, 480)

没有状态栏和 NO/YES FullScreenLayout view.frame 是 (0, 0, 320, 480) 并且 view.bounds 也是 (0, 0, 320, 480)


请帮忙解释一下这些东西,它只发生在设备上...

【问题讨论】:

  • 你考虑到状态栏了吗?尝试在您的视图控制器上设置wantsFullScreenLayout 并再次测试。
  • 带状态栏并且没有想要FullScreenLayout 范围是:[-25.5 .. 449]
  • 带状态栏和YES WantsFullScreenLayout 范围是:[-5.5 .. 469]
  • 没有状态栏和 NO/YES FullScreenLayout 范围是:[-5.5 .. 469]
  • 这很奇怪。请添加NSLog(@"%@", NSStringFromCGPoint(self.view)); 并发布输出

标签: iphone objective-c ios cocoa-touch uikit


【解决方案1】:

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

试试这个:

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

【讨论】:

  • 带状态栏并且没有想要FullScreenLayout 范围是:[-25.5 .. 449]
  • 带状态栏和YES WantsFullScreenLayout 范围是:[-5.5 .. 469]
  • 没有状态栏和 NO/YES FullScreenLayout 范围是:[-5.5 .. 469]
  • @user1229413 你为什么做同样的 cmets?
  • 哦,对不起。我是stackoverflow的新手。我的意思是我在有和没有状态栏的情况下测试了它。所有范围都很奇怪
【解决方案2】:

我是从 Apple 的手指绘画程序教程之一中找到的:

- (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 坐标才能获得您期望的结果。希望这会有所帮助。

【讨论】:

  • GLPaint 中也有同样的错误,如果你在这里写 NSLog(@"%@", NSStringFromCGPoint(location)) 你会得到错误的范围 - [-5,5 .. 469]跨度>
【解决方案3】:

根 viewController 的视图总是像竖屏模式。您应该在根视图内插入一个新视图。而且这个新视图会正确运行,会根据 Apple 的说法给出正确的大小和坐标。

例如;

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

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

touchPoint 是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    相关资源
    最近更新 更多