Question

I have UIViewController. And 3 UIView inside How detect touchs independently.

I have 3 classes, and added the objects in the UIViewController

And have this method in each class, I need touch the object (UIView) responds to the event independently

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

}

example:

  • view1 NSLog(@"I Touched View 1");

    view2 NSLog(@"I Touched View 2");

    view3 NSLog(@"I Touched View 3");

Thanks!!

Was it helpful?

Solution

If all of the three views are descendant of your viewController's view, you could use the following code snippet

for (UITouch *t in touches) {
    CGPoint p = [t locationInView:self.view];
    UIView *v = [self.view hitTest:p withEvent:event];
    NSLog(@"touched view %@", v);
}

EDIT

Ok, I supposed you had only one entry entry point (into the UIViewController) for touch detection of your subviews; If, like you said, you have a class for each subview, you have already solved your problem. You don't need to do anything else other than put your NSLog(@"touched..") code inside each touchBegan:withEvent: method.

E.g.

@implementation FirstSubview
.
.
-( void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"I touched View 1");
}
.
.
@end

Note: Since the UIViewController is also a UIResponder (i.e, inherits from UIResponder) you can also use the first solution I posted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top