使用iOS SwipeGesture循环浏览UIButtons

时间:2014-03-25 20:23:59

标签: ios loops uibutton hidden uiswipegesturerecognizer

我在UIView中设置了6个UIButtons,所有这些都位于完全相同的位置。我想要做的是从左到右或从右到左滑动,以便通过按钮。

我已经设置了UIGestures并在视图上工作,我只是不确定骑自行车穿过这些UIButton的最佳方式。

我在想,除了一个之外,标记每个UIButton和HIDE都很简单,但我不确定循环使用它们的最佳方法。

2 个答案:

答案 0 :(得分:1)

只需将它们放在NSMutableArray中,索引0处的任何按钮都是可见的,因为它们会滑动您删除索引0处的按钮,将其设置为hidden = YES并将其添加到然后,数组的结尾将按钮设置为索引0的hidden = NO

假设您在类的实现(.m)文件中使用ARC:

@interface MyFancyButtonClass () {
    NSMutableArray *_swipeButtons;
}

viewDidLoad

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

在你的gestureRecognizer中:

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

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

currentVisibleButton.hidden = YES;
nextVisibleButton.hidden = NO;

答案 1 :(得分:0)

像这样创建一个NSMutableArray:

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

将此数组保存到这样的属性

self.buttons = buttons;

存储currentButton像这样:

int currentButton = 0;

获取当前按钮:

UIButton * currentSelectedButton = buttons[currentButton];

循环显示如下按钮:

UIButton * currentSelectedButton = buttons[currentButton];
currentSelectedButton.hidden = YES;
currentButton++;
if (currentButton >= self.buttons.count)
    currentButton = 0;
currentSelectedButton = buttons[currentButton];
currentSelectedButton.hidden = NO;