views:

61

answers:

1

I am trying to detect touch on a particular area of screen where if user taps I can do something like this:

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

    UITouch *touch = [touches anyObject];
    CGPoint point=[touch locationInView:myView];
    NSLog(@"pointx: %f pointy:%f", point.x, point.y);
    if (CGRectContainsPoint(CGRectMake(5, 5, 40, 130),point));
    {
        NSLog(@"touched here");                     
    }   
}

But this message is displayed even when I touch anywhere on the screen. I want it to be displayed only when I touch myView.

I tried setting point.x and point.y to different numbers but that doesn't work? How can I solve this?

+1  A: 

The code quoted will always return YES because you are asking for the point within the view in question because points can have a negative location in a view. To get only touches in your view, use:

if ([touch view] == myView)
Run Loop
If an answer works, please accept it. This will encourage others to answer your questions too.
Run Loop
not working?? malso myview can b an imageview or a webView?? tried both with above but not working
hemant
if myView is a UIImageView, you have to add the following line:myView.userInteractionEnabled = YES;
Run Loop