I have multiple Views inside a ViewController and I want to know when a view is tapped on and I'm not sure how to go about it
Asked
Active
Viewed 207 times
-3
-
1I assume this is iOS, however the term "clicked" implies the use of a mouse. Please edit your tags. – trojanfoe Jun 24 '14 at 08:58
-
In iOS things are tapped. There is nothing to click. – Fogmeister Jun 24 '14 at 09:07
-
possible duplicate of [Detect if certain UIView was touched amongst other UIViews](http://stackoverflow.com/questions/2793242/detect-if-certain-uiview-was-touched-amongst-other-uiviews) – Hemang Jun 24 '14 at 09:07
-
Yeah could be, had trouble finding an answer – Byte Me Jun 24 '14 at 09:10
1 Answers
0
In order to check whether certain view inside another view was touched you can use hitTest.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
In your custom implementation of touchesBegan check every touch in touches set. The point for hitTest method can be obtained using
- (CGPoint)locationInView:(UIView *)view;
method, where the view is your superView (the one that contains other views).
EDIT: Here's a fast custom implementation:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint locationPoint = [[touches anyObject] locationInView:self];
UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];
}
I hope this was helpful, Paul
Sources : Detect if certain UIView was touched amongst other UIViews