PDA

View Full Version : How to detect touch that is outside a UIControl ?




namanhams
Jul 16, 2009, 11:22 AM
Let say i have a UIControl. What i want to do is, if the user tap on the control, then some action is executed. If the user tap anywhere else, then the UIControl disappears.

Actually it happens when you tap on a text field, then a menu pop up ("Select", "Select all", "Paste"). If you tap anywhere else, then the menu disappears.

Question is how can i detect that user touch somewhere else outside a specific control ?



TodVader
Jul 16, 2009, 01:59 PM
You place an invisible custom button behind everything and assign an action to it that does what you want. You stretch the button so it fills the entire screen.

mccannmarc
Jul 16, 2009, 04:18 PM
You place an invisible custom button behind everything and assign an action to it that does what you want. You stretch the button so it fills the entire screen.

That is pointless

@ The OP - Think about it, your UIControl is on a UIView & UIViews inherit from UIResponder. All you need to do is add the touch methods in the views class implementation. All touches outside of the UIControls on the view will be dealt with by the view class then.

PhoneyDeveloper
Jul 16, 2009, 08:22 PM
That is pointless

@ The OP - Think about it, your UIControl is on a UIView & UIViews inherit from UIResponder. All you need to do is add the touch methods in the views class implementation.

Basically what you're describing is a button. So what you're suggesting is write your own UIView subclass that does something when it's touched. Why is writing your own subclass better than just using a button for this?

Also, UIViewController is a subclass of UIResponder. You already have a UIViewController subclass. I'm pretty sure that if your container view doesn't handle a touch it's passed up to the view controller. So add the touch methods there if you're going to add them somewhere.

dejo
Jul 16, 2009, 09:09 PM
Basically what you're describing is a button. So what you're suggesting is write your own UIView subclass that does something when it's touched. Why is writing your own subclass better than just using a button for this?
I don't think they're suggesting subclassing a UIView. They're suggesting just adding touch handling to the view that is already there. The one that all the other views have been added to.

TodVader
Jul 16, 2009, 11:04 PM
I still think that using a button is the right solution for this. You can have multiple layers of this to have a different effect depending on where the user presses.

mccannmarc
Jul 17, 2009, 05:15 AM
Basically what you're describing is a button. So what you're suggesting is write your own UIView subclass that does something when it's touched. Why is writing your own subclass better than just using a button for this?

Also, UIViewController is a subclass of UIResponder. You already have a UIViewController subclass. I'm pretty sure that if your container view doesn't handle a touch it's passed up to the view controller. So add the touch methods there if you're going to add them somewhere.

I didn't suggest in any way to subclass a UIView to make it act like a UIButton, that would be utterly pointless. Why don't you make sure you understand my replies before you attack them in future? What I meant is what Dejo interpreted it as, if you didn't already have UIView subclass (Which I assume most people would) then fair enough you could add the methods to the view controller's implementation for the same result.

@ TodVader - Where multiple regions are concerned you could also use CGRects and check if the touch is within them. There is no right or wrong way of doing this. However your method is more memory hungry as you are allocating more objects