patryk
patryk

Reputation: 642

Generic way to detect a touch outside UIView frame

I have UIViewController with a custom UIView inside (lets call them VC and button). When button is touched, it's bounds and center changes with animation (it becomes bigger and presents a few options to choose from, after choice it resizes back). I would like to know how to "detect" (and dismiss the default action of that touch) a touch outside of the button (just to "hide" the button, in particular to make button resize to default smaller size).

Is there any generic way I could do that? VC has a lot of objects inside its view (table views, buttons, text fields, custom charts made with quartzcore, etc), do i need to block "interactivity" on all of those elements during the "big mode" of button?

Upvotes: 3

Views: 7023

Answers (2)

Gaurav Rastogi
Gaurav Rastogi

Reputation: 2145

Add a UIButton having the frame CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) as the first subview to your mainview (self.view) , add a selector to this button (@selector(backgroundTap)) , now add all other subviews like UITableView , UIButton , etc (as per your requirment) . Now whenever you will click on to the blank space where no Subview is present the backgroundTap selector will be called.

Upvotes: 3

Erik
Erik

Reputation: 5841

You could subclass what ever view you are using and expand the coordinates that will correspond to a touch in the view by implementing

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

Read more in the docs

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/hitTest:withEvent:

This way of implementing it is suggested in some WWDC videos from last year

Upvotes: 12

Related Questions