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 anyOb开发者_开发知识库ject];
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?
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)
精彩评论