I have a UIScrollView with paging enabled. In every page I have a button. I want that with every swipe the button that is showing in the UIScrollView
will be selected.
I'm guessing that the solution will be based on
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
but how can I know which button is showing without too many calculations of the range & offset?
Thanks
The easiest way is to set a tag for each UIButton from your UIScrollView that is equal with the pageNumber+1 (+1 because for the page 0 the tag will be 1 and all the defaults tags are 0 so you can find the button). Then you can compute the tag in viewDidEndDecelerating/viewDidScroll
or other scroll view delegate methods, depending on want you need. I'm doing the all the computation in viewDidEndDecelerating
something like this:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
//now you have the current page so you can get the button
UIButton *button =(UIButton *) [scrollView viewWithTag:page+1]; //the +1 that I was talking about above
button.selected = YES;
}
Also you may want to deselect the other buttons, so you can find them also by tags something like:
for(int i = 1; i<=numberOfPages; i++) {
UIButton *button =(UIButton *) [scrollView viewWithTag:i];
button.selected = NO;
}
But make sure you call the for
loop before the part where you are enable the button.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.