Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to detect someone tapping outside of a UIImageView
    primarykey
    data
    text
    <p>I have a UIImageView that is added as a subview. It shows up when a button is pressed.</p> <p>When someone taps outside of the UIImageView in any part of the application, I want the UIImageView to go away.</p> <pre><code>@interface SomeMasterViewController : UITableViewController &lt;clip&gt; &lt;clip&gt; @property (strong, nonatomic) UIImageView *someImageView; </code></pre> <p>There are some hints in stackoverflow and apple's documentation that sound like what I need. </p> <ul> <li><a href="http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html" rel="nofollow noreferrer">Apple's : Gesture Recognizers</a></li> <li><a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html" rel="nofollow noreferrer">Apple's : UIView hitTest:withEvent</a></li> <li><a href="http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITouch_Class/Reference/Reference.html" rel="nofollow noreferrer">Apple's : UITouch Class Reference</a></li> <li><a href="https://stackoverflow.com/questions/9780022/listening-to-uitouch-event-along-with-uigesturerecognizer">Stackoverflow: Listening to UITouch event along with UIGestureRecognizer</a></li> <li><a href="https://stackoverflow.com/questions/2221367/comparing-a-uitouch-location-to-uiimageview-rectangle">(not likely needed but..) - CGRectContainsPoint as mentioned in the following post titled: Comparing a UITouch location to UIImageView rectangle</a></li> </ul> <p>However, I want to check my approach here. It's my understanding that the code needs to </p> <ol> <li><p>Register a UITapGestureRecognizer to get all touch events that can happen in an application</p></li> <li><p>UITapGestureRecognizer should have its cancelsTouchesInView and delaysTouchesBegan and delaysTouchesEnded set to NO. </p></li> <li><p>Compare those touch events with the someImageView (how? Using UIView hitTest:withEvent?)</p></li> </ol> <p>Update: I am registering a UITapGestureRecognizer with the main UIWindow.</p> <p><strong>Final Unsolved Part</strong></p> <p>I have a handleTap:(UITapGestureRecognizer *) that the UITapGestureRecognizer will call. How can I take the UITapGestureRecognizer that is given and see if the tap falls outside of the UIImageView? Recognizer's locationInView looks promising, but I do not get the results I expect. I expect to see a certain UIImageView when I click on it and not see the UIImageView when I click in another spot. I get the feeling that the <strong>locationInView</strong> method is being used wrong.</p> <p>Here is my call to the locationInView method:</p> <pre><code>- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state != UIGestureRecognizerStateEnded) { NSLog(@"handleTap NOT given UIGestureRecognizerStateEnded so nothing more to do"); return; } UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window]; CGPoint point = [gestureRecognizer locationInView:mainWindow]; NSLog(@"point x,y computed as the location in a given view is %f %f", point.x, point.y); UIView *touchedView = [mainWindow hitTest:point withEvent:nil]; NSLog(@"touchedView = %@", touchedView); } </code></pre> <p>I get the following output:</p> <pre><code>&lt;clip&gt;point x,y computed as the location in a given view is 0.000000 0.000000 &lt;clip&gt;touchedView = &lt;UIWindow: 0x8c4e530; frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM; layer = &lt;UIWindowLayer: 0x8c4c940&gt;&gt; </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload