通过UIButtons阵列反向滑动

时间:2014-04-07 04:12:49

标签: ios uibutton nsmutablearray uiswipegesturerecognizer

在我的应用中,我有很多UIButtons叠加在一起。我想使用Swipe Gestures来回穿过它们。在viewDidLoad我有:

self.buttonArray = [[NSMutableArray alloc] initWithObjects:map, boardmembers, facebook, twitter, lessonbooks, schedule, nil];
    map.hidden = NO;
    boardmembers.hidden = facebook.hidden = twitter.hidden = lessonbooks.hidden = schedule.hidden = YES;

要从右向左滑动,然后推进它们:

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

    [_buttonArray removeObject:currentVisibleButton];
    [_buttonArray addObject:currentVisibleButton];

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

我遇到了相反的问题,我可以来回奔波。我该怎么做?

2 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解您的代码,但您发布的内容的类似相反顺序是:

UIButton *currentVisibleButton = [_buttonArray lastObject];
UIButton *nextVisibleButton = _buttonArray[_buttonArray.count-2];

[_buttonArray removeObject:currentVisibleButton];
[_buttonArray insertObject:currentVisibleButton atIndex:0];

但是使用数组作为环的更好方法是保留游标。保持当前可见索引的状态。

@property(assign, nonatomic) NSInteger cursor;

- (void)cursorLeft {
    self.cursor = (self.cursor+1 == _buttonArray.length)? 0 :  self.cursor+1;
}

- (void)cursorRight {
    self.cursor = (self.cursor == 0)? _buttonArray.length-1 :  self.cursor-1;
}

- (UIView *)viewAtCursor {
    return (UIView *)_buttonArray[self.cursor];
}

确保数组中索引为零的按钮可见,其他所有按钮都隐藏。现在没有对阵列进行任何搅动,你可以像这样滑动。

// swipe left
self.viewAtCursor.hidden = YES;
[self cursorLeft];
self.viewAtCursor.hidden = NO;

// swipe right
self.viewAtCursor.hidden = YES;
[self cursorRight];
self.viewAtCursor.hidden = NO;

答案 1 :(得分:0)

试试这个,

- (void)swipeToLeft:(BOOL) moveLeft {
    UIButton *currentVisibleButton = [_buttonArray firstObject];
    UIButton *nextVisibleButton;
    if (moveLeft) {
        nextVisibleButton = [_buttonArray objectAtIndex:1];
    } else {
        nextVisibleButton = [_buttonArray lastObject];
    }
    currentVisibleButton.hidden = YES;
    nextVisibleButton.hidden = NO;
    [_buttonArray removeObject:currentVisibleButton];
    [_buttonArray addObject:currentVisibleButton];
}