質問

In my app, I have a lot of UIButtons stack on top of each other. I want to use Swipe Gestures to go back and forth through them. in viewDidLoad I have:

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;

To swipe from right to left, and advance them I have:

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

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

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

I am having issues getting the reverse to work, where I can go back AND forth. How would I do this?

役に立ちましたか?

解決

I'm not sure I understand your code exactly, but the analogous reverse order of what you posted would be:

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

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

But a better way to use an array as a ring is to keep a cursor. Keep state of the currently visible index.

@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];
}

Make sure button at index zero in the array is visible and all the others are hidden. Now without doing any churning on the array, you can swipe around like this.

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

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

他のヒント

Try this,

- (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];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top