0

我有一个双应用程序,并将 touchesBegan 的代码块从 iPhone 代码复制到 iPad。我没有进行任何更改,并且该方法总是在第一次触摸时为 x 和 y 给我一个零。

代码块:

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

  int iCardId;

  NSSet *allTouches = [event allTouches];

  // we are dealing with one finger get first touch

  // get first touch
  UITouch *touch=[[allTouches allObjects] objectAtIndex:0];

  // get the x and y
  CGPoint pt    = [touch locationInView: self.view];
  float x=pt.x; // this is always a zero
  float y=pt.y; // this is always a zero
  .........
  .........
  .........
}
4

1 回答 1

0

我一直认为获得 a 的正确方法UITouch是使用[touches anyObject]. 试试看你是否仍然得到(0,0).

编辑/更新

我设置了一个新的单视图应用程序并将以下代码放入UIViewController

-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
    NSSet *allTouches = [event allTouches];
    // we are dealing with one finger get first touch
    // get first touch
    UITouch *touch=[[allTouches allObjects] objectAtIndex:0];
    // get the x and y
    CGPoint pt = [touch locationInView:[self view]];
    NSLog(@"(%f,%f)", pt.x, pt.y);
}

它似乎给了我适当的坐标。你把你的代码放在哪里了?

于 2012-05-07T23:36:43.397 回答