Skip to content

Instantly share code, notes, and snippets.

Created August 26, 2016 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d521bd41e97f32676bb747bacd0feb72 to your computer and use it in GitHub Desktop.
Save anonymous/d521bd41e97f32676bb747bacd0feb72 to your computer and use it in GitHub Desktop.
Scroll view project
//
// ViewController.m
// ThreeScrollViewProject
//
// Created by Echelon Creative on 8/25/16.
// Copyright © 2016 Energy Echelon. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
float screenWidth, scrollViewWidth, scrollViewHeight;
float vertScrollViewOffset, horizScrollViewOffset;
float topStart, topEnd, midStart, midEnd, botStart, botEnd;
float imgWidth, imgHeight;
int outfitCount;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
imgWidth = 1131;
imgHeight = 3244;
topStart = 1; // Really not needed and not used for the sake of simplicity.
topEnd = 1838;
midStart = 1320;
midEnd = 2987;
botStart = 2792;
botEnd = imgHeight;
screenWidth = self.view.bounds.size.width;
scrollViewWidth = 3.0 * screenWidth / 5.0;
scrollViewHeight = scrollViewWidth * imgHeight / imgWidth;
vertScrollViewOffset = 20; // Choose manually to shift the images away from status bar.
horizScrollViewOffset = screenWidth / 5.0;
outfitCount = 3;
// Manual initialization of three scrollviews. The calculations are based on 33% additional view
// provision on left and right of the main image. In other words, the page width is 3/5 of screenWidth
/////////////// Naked view starts here.
UIImageView *nakedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(horizScrollViewOffset, vertScrollViewOffset, scrollViewWidth, scrollViewHeight)];
nakedImageView.image = [UIImage imageNamed:@"naked.png"];
[self.view addSubview:nakedImageView];
/////////////// Bottom scrollview starts here ////////////////
UIScrollView *botScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(horizScrollViewOffset, vertScrollViewOffset + scrollViewHeight * botStart / imgHeight, scrollViewWidth, scrollViewHeight * (imgHeight - botStart + 1) / imgHeight)];
botScrollView.contentSize = CGSizeMake( (outfitCount + 2) * scrollViewWidth, botScrollView.frame.size.height);
UIImageView *botImageView = [[UIImageView alloc] init];
// Populate the first image
botImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth,botScrollView.frame.size.height)];
botImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bot%d.png",outfitCount-1]];
[botScrollView addSubview:botImageView];
// Populate last image
botImageView = [[UIImageView alloc] initWithFrame:CGRectMake( (outfitCount + 1) * scrollViewWidth, 0, scrollViewWidth,botScrollView.frame.size.height)];
botImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bot%d.png",0]];
[botScrollView addSubview:botImageView];
// Populate middle images
for (int i = 0; i < outfitCount; i++) {
botImageView = [[UIImageView alloc] initWithFrame:CGRectMake( (i + 1) * scrollViewWidth, 0, scrollViewWidth,botScrollView.frame.size.height)];
botImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bot%d.png",i]];
[botScrollView addSubview:botImageView];
}
[botScrollView setBackgroundColor:[UIColor clearColor]];
botScrollView.clipsToBounds = FALSE;
botScrollView.delegate = self;
[botScrollView setContentOffset:CGPointMake(scrollViewWidth,0)];
botScrollView.pagingEnabled = TRUE;
[self.view addSubview:botScrollView];
/////////////// Middle scrollview starts here ////////////////
UIScrollView *midScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(horizScrollViewOffset, vertScrollViewOffset + scrollViewHeight * midStart / imgHeight, scrollViewWidth, scrollViewHeight * (midEnd - midStart + 1) / imgHeight)];
midScrollView.contentSize = CGSizeMake( (outfitCount + 2) * midScrollView.frame.size.width, midScrollView.frame.size.height);
UIImageView *midImageView = [[UIImageView alloc] init];
// Populate the first image
midImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth,midScrollView.frame.size.height)];
midImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"mid%d.png",outfitCount-1]];
[midScrollView addSubview:midImageView];
// Populate last image
midImageView = [[UIImageView alloc] initWithFrame:CGRectMake( (outfitCount + 1) * scrollViewWidth, 0, scrollViewWidth,midScrollView.frame.size.height)];
midImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"mid%d.png",0]];
[midScrollView addSubview:midImageView];
// Populate middle images
for (int i = 0; i < outfitCount; i++) {
midImageView = [[UIImageView alloc] initWithFrame:CGRectMake( (i + 1) * scrollViewWidth, 0, scrollViewWidth,midScrollView.frame.size.height)];
midImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"mid%d.png",i]];
[midScrollView addSubview:midImageView];
}
[midScrollView setBackgroundColor:[UIColor clearColor]];
midScrollView.clipsToBounds = FALSE;
midScrollView.delegate = self;
[midScrollView setContentOffset:CGPointMake(scrollViewWidth,0)];
midScrollView.pagingEnabled = TRUE;
[self.view addSubview:midScrollView];
/////////////// Top scrollview starts here ////////////////
UIScrollView *topScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(horizScrollViewOffset, vertScrollViewOffset, scrollViewWidth, scrollViewHeight * topEnd / imgHeight)];
topScrollView.contentSize = CGSizeMake( (outfitCount + 2) * topScrollView.frame.size.width, topScrollView.frame.size.height);
UIImageView *topImageView = [[UIImageView alloc] init];
// Populate the first image
topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth,topScrollView.frame.size.height)];
topImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"top%d.png",outfitCount-1]];
[topScrollView addSubview:topImageView];
// Populate last image
topImageView = [[UIImageView alloc] initWithFrame:CGRectMake( (outfitCount + 1) * scrollViewWidth, 0, scrollViewWidth,topScrollView.frame.size.height)];
topImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"top%d.png",0]];
[topScrollView addSubview:topImageView];
// Populate middle images
for (int i = 0; i < outfitCount; i++) {
topImageView = [[UIImageView alloc] initWithFrame:CGRectMake( (i + 1) * scrollViewWidth, 0, scrollViewWidth,topScrollView.frame.size.height)];
topImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"top%d.png",i]];
[topScrollView addSubview:topImageView];
}
[topScrollView setBackgroundColor:[UIColor clearColor]];
topScrollView.clipsToBounds = FALSE;
topScrollView.delegate = self;
[topScrollView setContentOffset:CGPointMake(scrollViewWidth,0)];
topScrollView.pagingEnabled = TRUE;
[self.view addSubview:topScrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
if (sender.contentOffset.x < scrollViewWidth) {
[sender setContentOffset:CGPointMake( (outfitCount) * scrollViewWidth, 0) animated:NO];
}
else if (sender.contentOffset.x > (outfitCount) * scrollViewWidth) {
[sender setContentOffset:CGPointMake(scrollViewWidth, 0) animated:NO];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment