Iphone 触摸位置的值不可见';界

Iphone 触摸位置的值不可见';界,iphone,ios,ipad,touch,Iphone,Ios,Ipad,Touch,我试图记录触摸的位置。下面是我的代码。据我所知,在最远的左上角触摸一下,我会得到一个位置(0,0),而最远的右下角是(7681024),假设我拿着iPad的肖像。但是,我得到的值是左上角的(-6,-18)和右下角的(7611003)。看起来坐标有点偏移了。一个self.bounds的跟踪确实给了我{0,0},{768,1024}。有人能给我解释一下吗?我想得到{0,0},{768,1024}之间的x和y值。事先非常感谢 - (void)touchesBegan:(NSSet *)touches

我试图记录触摸的位置。下面是我的代码。据我所知,在最远的左上角触摸一下,我会得到一个位置(0,0),而最远的右下角是(7681024),假设我拿着iPad的肖像。但是,我得到的值是左上角的(-6,-18)和右下角的(7611003)。看起来坐标有点偏移了。一个self.bounds的跟踪确实给了我{0,0},{768,1024}。有人能给我解释一下吗?我想得到{0,0},{768,1024}之间的x和y值。事先非常感谢

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     CGRect bounds = [self bounds]; 
     NSLog(@"frame: %@", NSStringFromCGRect(bounds)); // this value was traced as frame: {{0, 0}, {768, 1024}}

     UITouch* touch = [[event touchesForView:self] anyObject]; 
     location = [touch locationInView:self];
     NSLog(@"Location: %@", NSStringFromCGPoint(location));

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     CGRect bounds = [self bounds]; 

     UITouch* touch = [[event touchesForView:self] anyObject]; 
     location = [touch locationInView:self];
     NSLog(@"Location: %@", NSStringFromCGPoint(location));

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

     CGRect bounds = [self bounds]; 

     UITouch* touch = [[event touchesForView:self] anyObject]; 
     location = [touch locationInView:self];
     NSLog(@"Location: %@", NSStringFromCGPoint(location));

}

因为我还不能发表评论,所以我必须发布一个答案。您是否尝试过将视图的背景色设置为其他颜色,以便您可以确定它是否位于左上角?

以下是我为视图提供的内容:

@implementation TouchesView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUserInteractionEnabled:YES];
        [self setBackgroundColor:[UIColor colorWithRed:1.0f green:0.6f blue:0.6f alpha:1.0f]];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touch = [[touches anyObject] locationInView:self];
    NSLog(@"(%.1f, %.1f)", touch.x, touch.y);
    NSLog(@"%@", NSStringFromCGPoint(touch));
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}

@end
这是初始化并将其添加到视图控制器:

TouchesView *touchView = [[TouchesView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:touchView];

这对我来说很好。

通过将“状态栏最初隐藏”设置为“是”,y值上的-20偏移量是固定的。剩下的问题看起来像是硬件问题,因为每次在iPad上运行程序时,x和y值的范围都不同。我在模拟器上运行程序时没有遇到这个问题。在模拟器上,x值范围从1到767,y值范围从1到1023,这是非常正确的。

这些触摸是否发生在主窗口上的视图完全偏移的视图控制器中?这些触摸方法在UIView的子类中实现。然后将此视图添加到视图控制器中。这个视图的x=0,y=0,width=768,height=1024(来自Size Inspector)。我猜您的界面构建不知怎么搞砸了,比如对齐不正确的视图或类似的视图。尝试创建一个全新的项目,看看它是否在那里工作。视图的框架是{{0,0},{768,1024}。在实例化视图时,尝试将视图的框架设置为视图控制器的边界:[view setFrame:self.view.bounds];是的,我尝试过将视图的背景色设置为不同的颜色:self.backgroundColor=[UIColor redColor],它位于左上角。此跟踪
CGRect thisFrame=[self frame];NSLog(@“THIS FRAME:%@”,NSStringFromCGRect(THIS FRAME))
确实给出了{0,0},{7681024}如果我将y值从Size Inspector更改为-20,触摸位置的y值似乎是正确的(范围从0到1024),但我不明白为什么??仍然无法使x值看起来正确(最左侧的触摸屏上x的值为6)。我的视图控制器有一个图像视图(作为背景)。具有触摸方法的视图将添加到imageview背景顶部的视图控制器中。尝试使用[self frame]而不是bounds,看看会发生什么。嗨,RileyE,感谢您的帮助!尝试此代码时,x和y值的范围是多少?你得到0-768和0-1024了吗?是的!我没有任何问题。您是否碰巧在后台运行音频或网络应用程序?这肯定会对坐标造成一些干扰(如果应用程序状态栏为up-personal hotspot或radio bar)。另外,你在你的应用程序中隐藏了状态栏吗?是的,我确实隐藏了状态栏,它从y中删除了-20偏移量。然而,我每次都会得到不同的x和y范围(-6761)和(01024);然后是(6768)和(-61017);以及其他。不过,我在模拟器上运行时没有问题。在模拟器上,我始终得到(1767)和(11023)。你觉得呢?嗨,RileyE,你在模拟器或iPad上得到了0-768和0-1024的范围吗?你真的得到了值0吗?不可能得到值0,因为没有人有大约一个像素宽的手指或手写笔。