#import "MyViewController.h"
static NSArray *__pageControlColorList = nil;
@implementation MyViewController
@synthesize pageImage;
// Creates the color list the first time this method is invoked. Returns one color object from the list.
+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index {
if (__pageControlColorList == nil) {
__pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor magentaColor],
[UIColor blueColor], [UIColor orangeColor], [UIColor brownColor], [UIColor grayColor], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]];
}
// Load the view nib and initialize the pageNumber ivar.
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
pageNumber = page;
}
return self;
}
- (void)dealloc {
//[pageNumberLabel release];
[pageImage release];
[super dealloc];
}
// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
//pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
//pageNumberLabel1.text = [NSString stringWithFormat:@"Sayfa %d", pageNumber + 1];
//self.view.backgroundColor = [MyViewController pageControlColorWithIndex:pageNumber];
CGRect myImageRect = CGRectMake(0, 0, 768, 1024);
self.pageImage = [[UIImageView alloc] initWithFrame:myImageRect];
NSString *imageName = [NSString stringWithFormat:@"%d.jpg",pageNumber];
[self.pageImage setImage:[UIImage imageNamed:imageName]];
self.pageImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:pageImage];
[self.pageImage release];
NSLog(@"bitti");
}
@end
V11ViewController.m
Code:
#import "V11ViewController.h"
#import "MyViewController.h"
#define ZOOM_VIEW_TAG 100
#define ZOOM_STEP 1.5
static NSUInteger kNumberOfPages = 288;
@interface V11ViewController (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end
@interface V11ViewController (UtilityMethods)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end
@implementation V11ViewController
@synthesize his_scrollView, viewControllers;
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
*/
- (void)viewDidLoad {
[super viewDidLoad];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
his_scrollView.pagingEnabled = YES;
his_scrollView.contentSize = CGSizeMake(his_scrollView.frame.size.width * kNumberOfPages, his_scrollView.frame.size.height);
his_scrollView.showsHorizontalScrollIndicator = NO;
his_scrollView.showsVerticalScrollIndicator = NO;
his_scrollView.scrollsToTop = NO;
his_scrollView.delegate = self;
his_scrollView.minimumZoomScale = 0.5;
his_scrollView.maximumZoomScale = 5.0;
[his_scrollView setBouncesZoom:YES];
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 1) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = his_scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[his_scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = his_scrollView.frame.size.width;
int page = floor((his_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
UIView *view = nil;
if (scrollView == his_scrollView) {
view = [self.his_scrollView viewWithTag:ZOOM_VIEW_TAG];
}
return view;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[his_scrollView setZoomScale:scale+0.01 animated:YES];
[his_scrollView setZoomScale:scale animated:YES];
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [his_scrollView frame].size.height / scale;
zoomRect.size.width = [his_scrollView frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
#pragma mark TapDetectingImageViewDelegate methods
- (void)tapDetectingImageView:(TapDetectingImageView *)view gotSingleTapAtPoint:(CGPoint)tapPoint {
// Single tap shows or hides drawer of thumbnails.
// [self toggleThumbView];
}
- (void)tapDetectingImageView:(TapDetectingImageView *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
// double tap zooms in
float newScale = [his_scrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
[his_scrollView zoomToRect:zoomRect animated:YES];
NSLog(@"double tap");
}
- (void)tapDetectingImageView:(TapDetectingImageView *)view gotTwoFingerTapAtPoint:(CGPoint)tapPoint {
// two-finger tap zooms out
float newScale = [his_scrollView zoomScale] / ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
[his_scrollView zoomToRect:zoomRect animated:YES];
NSLog(@"two finger tap");
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect frame = his_scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[his_scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[viewControllers release];
[his_scrollView release];
[pageControl release];
[super dealloc];
}
@end