-1

I have 6 UIButtons set up in my UIView, all in the exact same location. What I want to do, is to swipe from left to right or right to left, in order to go through the buttons.

I have the UIGestures all set up and working on the view, I am just not sure the best way to go about cycling through these UIButtons.

I was thinking that it could be simple enough to tag each UIButton and HIDE all but one, but am not sure about the best way to loop through these.

user717452
  • 33
  • 14
  • 73
  • 149

2 Answers2

1

Just put them in an NSMutableArray and whatever button is at index 0 is the visible one, as they swipe you'd remove the button at index 0, set it to hidden = YES and add it to the end of the array then set the button at index 0's hidden = NO.

Assuming you're using ARC, inside your class' implementation (.m) file:

@interface MyFancyButtonClass () {
    NSMutableArray *_swipeButtons;
}

inside your viewDidLoad:

_swipeButtons = [NSMutableArray arrayWithObjects:buttonOne, buttonTwo, buttonThree, buttonFour, nil];
buttonOne.hidden = NO;
buttonTwo.hidden = buttonThree.hidden = buttonFour.hidden = YES;

inside your gestureRecognizer:

UIButton *currentVisibleButton = [_swipeButtons firstObject];
UIButton *nextVisibleButton = [_swipeButtons objectAtIndex:1];

[_swipeButtons removeObject:currentVisibleButton];
[_swipeButtons addObject:currentVisibleButton];

currentVisibleButton.hidden = YES;
nextVisibleButton.hidden = NO;
Shizam
  • 9,627
  • 8
  • 51
  • 82
  • You could also animate them fading out by setting there .alpha = 0 in an animation block. – Adam Johnson Mar 25 '14 at 20:30
  • Agreed, that'd be the slick way of doing it. – Shizam Mar 25 '14 at 20:31
  • How do you add buttons to a mutable array and how to you change all these things. – user717452 Mar 25 '14 at 20:31
  • I kind of get what you're saying, but there's little to go on. – user717452 Mar 25 '14 at 20:33
  • I have ` self.buttonArray = [[NSMutableArray alloc] initWithObjects:map, boardmembers, facebook, twitter, lessonbooks, schedule, nil];` but am not sure about what to do with the array now. – user717452 Mar 25 '14 at 20:38
  • Sure thing, I updated the answer, my answer implies that the 1st item in the _swipeButtons is the visible button. – Shizam Mar 25 '14 at 20:50
  • I think I understand, but Xcode gives me an error on that last line. Says property hidden not found on object of type 'id'...regarding `[_swipeButtons firstObject].hidden = NO; – user717452 Mar 25 '14 at 20:52
  • Updated the answer, you could also solve it by doing `((UIButton *)[_swipeButtons firstObject]).hidden = NO` but I went with using more descriptive variable names instead. – Shizam Mar 25 '14 at 21:00
  • Would it be possible to do something in reverse for a different method when the swipe gesture recognizer for the opposite direction is called? – user717452 Mar 25 '14 at 21:11
  • Yes, just add/remove the items you need from _swipeButtons, the functions you'd be interested in are `[_swipeButtons lastObject]` to get the last item and `[_swipeButtons insertObject:button atIndex:0]` to put a button on top of the stack instead of the bottom. – Shizam Mar 25 '14 at 21:44
  • I have added for that gesture `UIButton *currentVisibleButton = [_buttonArray lastObject]; UIButton *nextVisibleButton = [_buttonArray objectAtIndex:0]; [_buttonArray removeObject:currentVisibleButton]; [_buttonArray addObject:currentVisibleButton]; currentVisibleButton.hidden = YES; nextVisibleButton.hidden = NO;` but it didn't do anything other than print the log in the console. – user717452 Mar 26 '14 at 01:59
  • I still cannot figure out how to get it to go backwards. @Shizam – user717452 Apr 07 '14 at 04:07
0

Create a NSMutableArray like this:

NSMutableArray * buttons = [[NSMutableArray alloc] init];
[buttons addObject:button1];
[buttons addObject:button2];
[buttons addObject:button3];
[buttons addObject:button4];
[buttons addObject:button5];

Save this array to a property like this

self.buttons = buttons;

Store the currentButton Like this:

int currentButton = 0;

Get the current button like this:

UIButton * currentSelectedButton = buttons[currentButton];

Cycle through the buttons like this:

UIButton * currentSelectedButton = buttons[currentButton];
currentSelectedButton.hidden = YES;
currentButton++;
if (currentButton >= self.buttons.count)
    currentButton = 0;
currentSelectedButton = buttons[currentButton];
currentSelectedButton.hidden = NO;
Jack
  • 16,677
  • 8
  • 47
  • 51
  • Not sure if this works for my setup. I use .xib and not storyboard. The 6 buttons are laid out on my view, all in the same coordinates and size. I have two GestureRecognizers set up, one for left swipe and one for right swipe. They each call a different IBAction set up in my code. – user717452 Mar 25 '14 at 20:45
  • Create the array in ViewDidLoad: Connect all your buttons as IBOutlets to your code and you're good to go. If you're feeling adventurous, create a IBOutletCollection and your array of buttons is automatically created for you! – Jack Mar 25 '14 at 21:06