Preventing multiple buttons from being touched at the same time

前端 未结 6 1494
猫巷女王i
猫巷女王i 2021-02-01 02:24

In iOS is there anyway to prevent a UIView containing multiple buttons (siblings) from being simultaneously from being touched? For instance, two non-overlapping buttons that ar

相关标签:
6条回答
  • 2021-02-01 02:33

    You need to find all buttons on that view and set the "exclusiveTouch" property to true in order to prevent multi touch at the same time.

    func exclusiveTouchForButtons(view: UIView) {
        for cmp in view.subviews {
            if let cmpButton = cmp as? UIButton {
                cmpButton.exclusiveTouch = true
            } else {
                exclusiveTouchForButtons(cmp)
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-01 02:34
    I have tried both  multiTouchEnabled and exclusiveTouch but unfortunately 
    none of them workout for me.I have tried the following code worked 
    perfectly.
    

    Set this code at .h file

    BOOL ClickedBool;
    

    Set the following code at method start.

    if(ClickedBool==TRUE)
    {
         return;
    }
    else
    {
        ClickedBool=TRUE;
    }
    
    0 讨论(0)
  • 2021-02-01 02:47
    for(UIView* v in self.view.subviews)
        {
        if([v isKindOfClass:[UIButton class]])
        {
            UIButton* btn = (UIButton*)v;
            [yourButton setExclusiveTouch:YES];
        }
    }
    
    0 讨论(0)
  • 2021-02-01 02:51

    Set UIView.exclusiveTouch.

    0 讨论(0)
  • 2021-02-01 02:56

    Swift 4 Syntax:

        buttonA.isExclusiveTouch = true
        buttonB.isExclusiveTouch = true
    
    0 讨论(0)
  • 2021-02-01 02:58

    You can also use below method. If you have two buttons or more, to prevent multiple push at a time.

    for e.g,

    [Button1 setExclusiveTouch:YES];
    
    [Button2 setExclusiveTouch:YES];
    

    Set this method in your viewDidLoad or viewWillAppear

    0 讨论(0)
提交回复
热议问题