简体   繁体   中英

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM