Jan
Jan

Reputation: 273

Detect UIButton order?

In the app I'm currently working on, I need to drag and drop UIButtons. This is working without problem. The issue I'm facing is that I would like to somehow recognize their orders. For exemple on my Storyboard, I have in the view those three buttons tagged with numbers :

|2| |3| |1|

I would like to be able to tell the user whether or not the buttons are in the good order. Is there a way to achieve this ?

Many thanks in advance ;)

Upvotes: 0

Views: 71

Answers (1)

Geek
Geek

Reputation: 8320

I assume that you wan to check order horizontally. Whenever you want to check the order, just get the button.frame.origin.x for all buttons and compare them.
I consider that correct order is 1 | 2 |3. Call below method whenever you want to check the order :

- (void)checkOrder {
    positionOfbutton1 = button1.frame.origin.x;
    positionOfbutton2 = button2.frame.origin.x;
    positionOfbutton3 = button3.frame.origin.x;

    if (positionOfbutton1 > positionOfbutton2 || positionOfbutton2 > positionOfbutton3)
    {
        // Show alert that buttons are disordered
    }
}

Upvotes: 4

Related Questions