How to detect touch that is outside a UIControl ?

Discussion in 'iOS Programming' started by namanhams, Jul 16, 2009.

  1. namanhams macrumors regular

    Joined:
    Jun 3, 2009
    #1
    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 ?
     
  2. TodVader macrumors 6502a

    TodVader

    Joined:
    Sep 27, 2005
    Location:
    Quebec, Canada
    #2
    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.
     
  3. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #3
    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.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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.
     
  6. TodVader macrumors 6502a

    TodVader

    Joined:
    Sep 27, 2005
    Location:
    Quebec, Canada
    #6
    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.
     
  7. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #7
    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
     

Share This Page