-
-
Save siqin/2923259 to your computer and use it in GitHub Desktop.
Some sample code for a horizontal paged UIScrollView with a gap between each image, like Photos.app (which looks nicer when paging).
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
/* | |
Some sample code for a horizontal paged UIScrollView with a gap between each image (which looks nicer). Note - not using contentInset, we want each page to fill the iPhone screen and the gap only to be visible when scrolling (like Photos.app). | |
Taking the iPhone status bar into account - our viewport is 320x460 | |
Our UIScrollView is therefore set in Interface Builder to 340x460 with a -10 X offset (so it's larger than the viewport but still centred) | |
Then we do the following... | |
*/ | |
// Our image filenames | |
NSArray *imgNames = [[NSArray alloc] initWithObjects:@"0.jpg", @"1.jpg", @"2.gif", @"3.jpg", @"4.jpg", @"5.png", @"6.jpg", @"7.jpg", @"8.jpg", @"9.png", @"10.jpg", nil]; | |
// Setup the array of UIImageViews | |
imgArray = [[NSMutableArray alloc] init]; | |
UIImageView *tempImageView; | |
for(NSString *name in imgNames) { | |
tempImageView = [[UIImageView alloc] init]; | |
tempImageView.contentMode = UIViewContentModeScaleAspectFit; | |
tempImageView.image = [UIImage imageNamed:name]; | |
[imgArray addObject:tempImageView]; | |
[tempImageView release]; | |
} | |
CGSize pageSize = scrollView.frame.size; // scrollView is an IBOutlet for our UIScrollView | |
NSUInteger page = 0; | |
for(UIView *view in imgArray) { | |
[scrollView addSubview:view]; | |
// This is the important line | |
view.frame = CGRectMake(pageSize.width * page++ + 10, 0, pageSize.width - 20, pageSize.height); | |
// We're making use of the scrollView's frame size (pageSize) so we need to; | |
// +10 to left offset of image pos (1/2 the gap) | |
// -20 for UIImageView's width (to leave 10 gap at left and right) | |
} | |
scrollView.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment