Skip to content

Instantly share code, notes, and snippets.

@jimjeffers
Created May 24, 2012 22:54
Show Gist options
  • Save jimjeffers/2784727 to your computer and use it in GitHub Desktop.
Save jimjeffers/2784727 to your computer and use it in GitHub Desktop.
Figuring out how to juggle UIImageViews as a UIScrollview scrolls.
- (void)viewDidLoad
{
[super viewDidLoad];
// We'll want the controller to become the scrollView's delegate
// in order to respond to changes in the content offset position.
shelfScrollView.delegate = self;
// We'll generate an array of image views to serve as the shelf
// background. These shelves will reposition as they scroll out
// of the viewport.
shelves = [[NSMutableArray alloc] init];
for (int shelvesToCreate = 7; shelvesToCreate > 0; shelvesToCreate--) {
UIImageView *shelfImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SteelShelfLandscape.png"]];
[shelves addObject:shelfImage];
}
topShelfIndex = 0;
bottomShelfIndex = 6;
}
- (void)viewWillAppear:(BOOL)animated {
// We'll set the position of the shelves without using an animation
// whenever the view is about to appear. This ensures the proper
// orientation is presented at any given time.
shelfScrollView.contentSize = CGSizeMake(shelfScrollView.frame.size.width, shelfScrollView.frame.size.height*2);
// The shelves are actually image views rather than a background
// pattern. This is required for performance reasons. Using a large
// image as a background pattern causes the device framerate to drop
// to nearly 6fps. -JJ
UIImage *image;
UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
image = [UIImage imageNamed:@"SteelShelfLandscape.png"];
} else {
image = [UIImage imageNamed:@"SteelShelf.png"];
}
NSEnumerator *shelfEnumerator = [shelves objectEnumerator];
UIImageView *shelfImage;
float currentShelf = 0.0;
while (shelfImage = [shelfEnumerator nextObject]) {
shelfImage.image = image;
[shelfImage setFrame:CGRectMake(0, currentShelf*shelfImage.frame.size.height, shelfScrollView.bounds.size.width, shelfImage.frame.size.height)];
[shelfScrollView addSubview:shelfImage];
currentShelf++;
}
}
#pragma mark - Scrollview Delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIImageView *shelf = [shelves objectAtIndex:0];
if (scrollView.contentOffset.y < topShelfIndex*shelf.bounds.size.height) {
topShelfIndex--;
bottomShelfIndex--;
// We'll assign the first shelf in the array as the lowest and continue
// to test and reassign the lowest shelf until we've exhausted all
// possible options.
UIImageView *lowestImageView = shelf;
UIImageView *currentShelfImage;
NSEnumerator *shelfEnumerator = [shelves objectEnumerator];
while (currentShelfImage = [shelfEnumerator nextObject]) {
if (currentShelfImage.frame.origin.y > lowestImageView.frame.origin.y) {
lowestImageView = currentShelfImage;
}
}
[lowestImageView setFrame:CGRectMake(0, lowestImageView.bounds.size.height*topShelfIndex, scrollView.bounds.size.width, lowestImageView.bounds.size.height)];
} else if (scrollView.contentOffset.y + scrollView.bounds.size.height > bottomShelfIndex*shelf.bounds.size.height) {
topShelfIndex++;
bottomShelfIndex++;
// We'll assign the first shelf in the array as the topmost and continue
// to test and reassign the topmost shelf until we've exhausted all
// possible options.
UIImageView *highestImageView = shelf;
UIImageView *currentShelfImage;
NSEnumerator *shelfEnumerator = [shelves objectEnumerator];
while (currentShelfImage = [shelfEnumerator nextObject]) {
if (currentShelfImage.frame.origin.y < highestImageView.frame.origin.y) {
highestImageView = currentShelfImage;
}
}
[highestImageView setFrame:CGRectMake(0, highestImageView.bounds.size.height*bottomShelfIndex, scrollView.bounds.size.width, highestImageView.bounds.size.height)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment