tags:

views:

261

answers:

2

I have a viewcontroller like the following. But the touchsBegan doestnt get detected. Can anyone plz tell me what is wrong.

- (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  A: 

Touches are only called on UIViews. This means you have to subclass UIView and put your touchesBegan:withEvent: code in this subclass. You're using the code inside an UIViewController and this doesn't get any touches itself because it's not an object on screen. A controller is just for the application logic behind a view.

bddckr
Can you please tell me how to create a UIView subclass
Muniraj
Have a look on Apple docs for this. It also seems to me, that you're trying to use a tabbar. You don't check for the position of the touch yourself. Each `UIView` subclass will to that for you. Just read the docs:http://developer.apple.com/iphone/library/navigation/index.html
bddckr
Actually the above code is to create a overlay image to a playing movie. That is why i need to check for position instead of a tabbar.
Muniraj
i m not sure if u can detect touch over the movie thing.. unless u subclass a view and then detect the touch there..
BK
A: 

As chriB said touches are detected only on views and as u access it in a controller its not detected.

also FYI, the touches wont be detected on any views you add over the base view.

ie: if u have a view and u add a scroll view or a table view over it, then the touches wont be detected over this added regions. same goes for buttons, textfields etc. Touch is detected only wen its done exactly on the [naked ;)] view.

BK
hey i realized jus now, check where you are touching on the screen. i mean hope u are not touching on a button,textfield or a added view.
BK