I have 3 UIViews, layered on top of one large uiview. I want to know if the user touches the top one and not care about the other ones. I will have a couple of buttons in th
This worked for me:
func respondToGesture(gesture: UIGestureRecognizer) {
let containsPoint = CGRectContainsPoint(targetView.bounds, gesture.locationInView(targetView))
}
In my case the UIView I wanted to find was not returned by the hitTest. But the following code worked better:
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
CGPoint viewPoint = [myImage convertPoint:locationPoint fromView:self.view];
if ([myImage pointInside:viewPoint withEvent:event]) {
// do something
}
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
swift code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
if touch.view == self.view {
self.hideInView()
} else {
return
}
}
}
My swift version to check that touch took place in infoView:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first, infoView.bounds.contains(touch.location(in: infoView)) else { return }
print("touchesBegan") // Replace by your own code
} // touchesBegan
In touchesBegan check that the touched view is the one you want, solved the problem for me when I tried to identified if the user touched a specific ImageView.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touch view] == imageView ) {
... your code to handle the touch
}
...
If you adjust the content for the view diamensions make sure to change the view size accordingly otherwise it will recognize touch event even in area where there in so content in the view. In my case when I tried to keep the image proportion with UIViewContentModeScaleAspectFit although the image was adjusted as required in the "white spaces" areas touch event were catched as well.