touchesBegan没有被发现

时间:2010-03-17 10:14:06

标签: iphone

我有一个如下的viewcontroller。但是touchesBegan没有被发现。 任何人都可以告诉我有什么问题。

- (id)init
{

  if (self = [super init])
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];    
  return self;  
}

-(void) viewWillAppear:(BOOL)animated

{
  overlay = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlay.png"]] autorelease];

  [self.view addSubview:overlay]; 

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{     
  // Detect touch anywhere

    UITouch *touch = [touches anyObject];

    // Where is the point touched

  CGPoint point = [touch locationInView:self.view]; 
  NSLog(@"pointx: %f pointy:%f", point.x, point.y);

  // Was a tab touched, if so, which one...

  if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point))

      NSLog(@"tab 1 touched");

    else if (CGRectContainsPoint(CGRectMake(107, 440, 106, 40), point))

      NSLog(@"tab 2 touched");

    else if (CGRectContainsPoint(CGRectMake(214, 440, 106, 40), point))

      NSLog(@"tab 3 touched");

}

2 个答案:

答案 0 :(得分:2)

仅在UIView上调用触摸。这意味着您必须继承UIView并将touchesBegan:withEvent:代码放在此子类中。您正在使用UIViewController内的代码,而这本身并没有任何触摸,因为它不是屏幕上的对象。控制器仅用于视图后面的应用程序逻辑。

答案 1 :(得分:0)

正如chriB所说,仅在视图上检测到触摸,并且当您在控制器中访问它时未检测到触摸。

另外,在基础视图中添加的任何视图都不会检测到触摸。

即:如果你有一个视图,你在它上面添加一个滚动视图或一个表视图,那么就不会在这个添加的区域上检测到触摸。对于按钮,文本字段等也是如此。仅在基本视图上检测到触摸才会完成。