hemant
hemant

Reputation: 195

detecting touch on a particular area of screen

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?

Upvotes: 1

Views: 544

Answers (1)

RunLoop
RunLoop

Reputation: 20376

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)

Upvotes: 1

Related Questions