Allow Only One Button To Be Pressed At A Time?

Discussion in 'iOS Programming' started by Darkroom, Aug 26, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    so this is how i'm going to try and implement this:

    1. throw all my UIButtons into a mutable array.
    2. while one button is pressed, remove it from the mutable array.
    3. fast enumerate thru the array and cancel all button presses.
    4. on touchesEnded add the pressed button back into the mutable array.

    in the event that 2 buttons are pressed at once, this would be to insure that only one button can be pressed at a time, so that multiple actions don't execute (and cause animation/visual errors).

    but i'd like to know if there is a better way to do this?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    How about this?: When the first button is pressed, set a BOOL to YES. If you receive any other button presses and that BOOL is YES, cancel the new button press.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    i think i tried that, but found that it was still possible to touch 2 buttons at once, and that a cancel button pressed method was required. or maybe i just coded it wrong / imagined things?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    ohhh... nice...

    not totally clear after reading the docs on exclusiveTouch, though... i'll have a lot of work removing bools and other variables to impliment exclusiveTouch, so before i start – i have a full screen that accepts touch events, evaluates CGPoint distances and moves a UIImageView accordingly. above the UIImageView i have UIButtons. can i simply insert the following code to make this work?:

    Code:
    //loadView
    [self.view setExclusiveTouch:YES];
    [self.button1 setExclusiveTouch:YES];
    [self.button2 setExclusiveTouch:YES];
    [self.button3 setExclusiveTouch:YES];
    
    so if the buttons are not being touched, the main view is (since it's the entire screen). but if button1 is touched, it will cancel all previous touches (touchesCancel connection in IB?) and disallow touching of the main view or the other buttons until touchesEnded?
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    I haven't used the exclusiveTouch property so I'm not certain. The way I read the docs if you set it on your buttons then once a button is touched no other button can send a touch. I don't think it's essential that you set this property for all views in a screen. I guess I'm not sure what happens if you first touch a view that's not exclusive and then touch one that is.

    It should be simple enough to figure it out though.
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    my code above works. however, it seems that if you only have 2 buttons (or views) i think that only one needs to be set with the exclusiveTouch property in order for both to benefit.
     

Share This Page