0

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!!

Fabio
  • 1,913
  • 5
  • 29
  • 53
  • I think I understood correctly your problem now, if that would not be the case, please be more precise. – HepaKKes Jun 24 '13 at 22:19

1 Answers1

4

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.

HepaKKes
  • 1,555
  • 13
  • 22
  • also, try to take a look at [this related question](http://stackoverflow.com/questions/2793242/detect-if-certain-uiview-was-touched-amongst-other-uiviews?rq=1) – HepaKKes Jun 24 '13 at 21:26
  • thanks... this works very well! But when touches UIView 1, how can i do to the event is captured by the father? it's possible?? For example Touch UIView 1 and the (father) "set" viewcontroller.alpha=0 Sorry for my english... – Fabio Jun 25 '13 at 00:44
  • there are several way to do that, you could use, e.g, the first solution I mentioned or call back the view controller through a delegate (see [link](http://developer.apple.com/library/ios/#documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html)) or use the more standard approach which basically consists on adding to your `UIButton`s a target\action through `-addTarget:action:forControlEvent` (see [link](http://developer.apple.com/library/ios/#documentation/general/conceptual/CocoaEncyclopedia/Target-Action/Target-Action.html)) – HepaKKes Jun 25 '13 at 10:01