Skip to content

Instantly share code, notes, and snippets.

@pnicholls
Created July 2, 2011 04:47
Show Gist options
  • Save pnicholls/1059743 to your computer and use it in GitHub Desktop.
Save pnicholls/1059743 to your computer and use it in GitHub Desktop.
//
// WallrViewController.m
// Wallr
//
// Created by Peter Nicholls on 2/07/11.
// Copyright 2011 NA. All rights reserved.
//
#import "WallrViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation WallrViewController
@synthesize scrollView;
@synthesize pageControl;
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupPage];
}
- (void)setupPage
{
scrollView.delegate = self;
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSMutableArray *images = [[NSMutableArray alloc] init];
[images addObject:[UIImage imageNamed:@"image1.png"]];
[images addObject:[UIImage imageNamed:@"image2.png"]];
[images addObject:[UIImage imageNamed:@"image3.jpg"]];
[images addObject:[UIImage imageNamed:@"image4.png"]];
[images addObject:[UIImage imageNamed:@"image1.png"]];
[images addObject:[UIImage imageNamed:@"image2.png"]];
[images addObject:[UIImage imageNamed:@"image3.jpg"]];
[images addObject:[UIImage imageNamed:@"image4.png"]];
NSUInteger nimages = images.count;
CGFloat cx = 0;
for (UIImage *image in images) {
if (image == nil) {
break;
}
CGRect myImageRect = CGRectMake(185.0f, 135.0f, 400.0f, 300.0f);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView setImage:image];
[imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[imageView.layer setBorderWidth: 5.0];
imageView.layer.shadowColor = [UIColor grayColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(0, 1);
imageView.layer.shadowOpacity = 1;
imageView.layer.shadowRadius = 1.0;
imageView.opaque = YES; // explicitly opaque for performance
imageView.userInteractionEnabled = YES;
CGRect rect = imageView.frame;
rect.origin.x = 185.0f + cx;
rect.origin.y = 135.0f;
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage) {
return;
}
/*
* We switch page at 50% across
*/
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender
{
/*
* Change the scroll view
*/
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
/*
* When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
*/
pageControlIsChangingPage = YES;
}
- (void)viewDidUnload
{
[scrollView release];
[pageControl release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment