Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how identify touch in a particular view

Tags:

iphone

uitouch

How to get touch on a particular view.

I am using

CGPoint Location = [[touches anyObject] locationInView:self.view ];

but want to trigger the action only if an particular subView is clicked.
How to do this.

like image 760
Kumar sonu Avatar asked Mar 04 '11 10:03

Kumar sonu


4 Answers

I got the answer myself...but thanks other which helped me on this

here it is

UITouch *touch ;
touch = [[event allTouches] anyObject];


    if ([touch view] == necessarySubView)
{
//Do what ever you want
}
like image 192
Kumar sonu Avatar answered Sep 18 '22 12:09

Kumar sonu


Try this

//here enable the touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // get touch event
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    if (CGRectContainsPoint(yoursubview_Name.frame, touchLocation)) {
        //Your logic
        NSLog(@" touched");
    }
}
like image 32
Sat Avatar answered Sep 20 '22 12:09

Sat


You should create a subclass (or create a category) of UIView and override the

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

where redirect the message to the appropriate delegate.

like image 20
Max Avatar answered Sep 22 '22 12:09

Max


// here Tiles is a separate class inherited from UIView Class
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([[touch view] isKindOfClass:[Tiles class]]) {
        NSLog(@"[touch view].tag = %d", [touch view].tag);
    }
}

like this you can find view or subview is touched

like image 41
User-1070892 Avatar answered Sep 18 '22 12:09

User-1070892