开发者

detecting touch on a particular area of screen

开发者 https://www.devze.com 2023-01-04 15:59 出处:网络
I am trying to detect touch on a particular area of screen where if user taps I can do something like this:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消