Created
May 28, 2012 17:52
-
-
Save jacksonfdam/2820287 to your computer and use it in GitHub Desktop.
How to implement UIScrollView with 1000+ subviews?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface UntitledViewController : UIViewController <UIScrollViewDelegate> | |
{ | |
@private | |
UIScrollView *_scrollView; | |
NSArray *_objects; | |
int refPage, currentPage; | |
UILabel *_detailLabel1; | |
UILabel *_detailLabel2; | |
UILabel *_detailLabel3; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface UntitledViewController () | |
- (void)replaceHiddenLabels; | |
- (void)displayLabelsAroundIndex:(NSInteger)index; | |
@end | |
@implementation UntitledViewController | |
- (void)dealloc | |
{ | |
[_objects release]; | |
[_scrollView release]; | |
[_detailLabel1 release]; | |
[_detailLabel2 release]; | |
[_detailLabel3 release]; | |
[super dealloc]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
refpage = 0; | |
curentPage = 0; | |
_objects = [[NSArray alloc] initWithObjects:@"first", @"second", @"third", | |
@"fourth", @"fifth", @"sixth", @"seventh", @"eight", @"ninth", @"tenth", nil]; | |
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)]; | |
_scrollView.contentSize = CGSizeMake(320.0 * [_objects count], 460.0); | |
_scrollView.showsVerticalScrollIndicator = NO; | |
_scrollView.showsHorizontalScrollIndicator = YES; | |
_scrollView.alwaysBounceHorizontal = YES; | |
_scrollView.alwaysBounceVertical = NO; | |
_scrollView.pagingEnabled = YES; | |
_scrollView.delegate = self; | |
_detailLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)]; | |
_detailLabel1.textAlignment = UITextAlignmentCenter; | |
_detailLabel1.font = [UIFont boldSystemFontOfSize:30.0]; | |
_detailLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(320.0, 0.0, 320.0, 460.0)]; | |
_detailLabel2.textAlignment = UITextAlignmentCenter; | |
_detailLabel2.font = [UIFont boldSystemFontOfSize:30.0]; | |
_detailLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(640.0, 0.0, 320.0, 460.0)]; | |
_detailLabel3.textAlignment = UITextAlignmentCenter; | |
_detailLabel3.font = [UIFont boldSystemFontOfSize:30.0]; | |
// We are going to show all the contents of the _objects array | |
// using only these three UILabel instances, making them jump | |
// right and left, replacing them as required: | |
[_scrollView addSubview:_detailLabel1]; | |
[_scrollView addSubview:_detailLabel2]; | |
[_scrollView addSubview:_detailLabel3]; | |
[self.view addSubview:_scrollView]; | |
} | |
- (void)viewDidAppear:(BOOL)animated | |
{ | |
[super viewDidAppear:animated]; | |
[_scrollView flashScrollIndicators]; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
[self displayLabelsAroundIndex:0]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Here you could release the data source, but make sure | |
// you rebuild it in a lazy-loading way as soon as you need it again... | |
[super didReceiveMemoryWarning]; | |
} | |
#pragma mark - | |
#pragma mark UIScrollViewDelegate methods | |
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView | |
{ | |
// Do some initialization here, before the scroll view starts moving! | |
} | |
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView | |
{ | |
[self replaceHiddenLabels]; | |
} | |
- (void)displayLabelsAroundIndex:(NSInteger)index | |
{ | |
NSInteger count = [_objects count]; | |
if (index >= 0 && index < count) | |
{ | |
NSString *text = [_objects objectAtIndex:index]; | |
_detailLabel1.frame = CGRectMake(320.0 * index, 0.0, 320.0, 460.0); | |
_detailLabel1.text = text; | |
[_scrollView scrollRectToVisible:CGRectMake(320.0 * index, 0.0, 320.0, 460.0) animated:NO]; | |
if (index < (count - 1)) | |
{ | |
text = [_objects objectAtIndex:(index + 1)]; | |
_detailLabel2.frame = CGRectMake(320.0 * (index + 1), 0.0, 320.0, 460.0); | |
_detailLabel2.text = text; | |
} | |
if (index > 0) | |
{ | |
text = [_objects objectAtIndex:(index - 1)]; | |
_detailLabel3.frame = CGRectMake(320.0 * (index - 1), 0.0, 320.0, 460.0); | |
_detailLabel3.text = text; | |
} | |
} | |
} | |
- (void)replaceHiddenLabels | |
{ | |
static const double pageWidth = 320.0; | |
NSInteger currentIndex = ((_scrollView.contentOffset.x - pageWidth) / pageWidth) + 1; | |
UILabel *currentLabel = nil; | |
UILabel *previousLabel = nil; | |
UILabel *nextLabel = nil; | |
if (CGRectContainsPoint(_detailLabel1.frame, _scrollView.contentOffset)) | |
{ | |
currentLabel = _detailLabel1; | |
previousLabel = _detailLabel2; | |
nextLabel = _detailLabel3; | |
} | |
else if (CGRectContainsPoint(_detailLabel2.frame, _scrollView.contentOffset)) | |
{ | |
currentLabel = _detailLabel2; | |
previousLabel = _detailLabel1; | |
nextLabel = _detailLabel3; | |
} | |
else | |
{ | |
currentLabel = _detailLabel3; | |
previousLabel = _detailLabel1; | |
nextLabel = _detailLabel2; | |
} | |
currentLabel.frame = CGRectMake(320.0 * currentIndex, 0.0, 320.0, 460.0); | |
currentLabel.text = [_objects objectAtIndex:currentIndex]; | |
// Now move the other ones around | |
// and set them ready for the next scroll | |
if (currentIndex < [_objects count] - 1) | |
{ | |
nextLabel.frame = CGRectMake(320.0 * (currentIndex + 1), 0.0, 320.0, 460.0); | |
nextLabel.text = [_objects objectAtIndex:(currentIndex + 1)]; | |
} | |
if (currentIndex >= 1) | |
{ | |
previousLabel.frame = CGRectMake(320.0 * (currentIndex - 1), 0.0, 320.0, 460.0); | |
previousLabel.text = [_objects objectAtIndex:(currentIndex - 1)]; | |
} | |
} | |
- (void)scrollViewDidScroll:(UIScrollView *)sender{ | |
int currentPosition = floor(_scrollView.contentOffset.x); | |
currentPage = MAX(0,floor(currentPosition / 340)); | |
//340 is the width of the scrollview... | |
if(currentPage != refPage) { | |
refPage = currentPage; | |
[self replaceHiddenLabels]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From: http://stackoverflow.com/questions/2079225/how-to-implement-uiscrollview-with-1000-subviews