diff --git a/AmzPhoto/AmzPhoto/PagePhotos/1933_1.jpg b/AmzPhoto/AmzPhoto/PagePhotos/1933_1.jpg new file mode 100755 index 0000000..903ed2d Binary files /dev/null and b/AmzPhoto/AmzPhoto/PagePhotos/1933_1.jpg differ diff --git a/AmzPhoto/AmzPhoto/PagePhotos/1933_2.jpg b/AmzPhoto/AmzPhoto/PagePhotos/1933_2.jpg new file mode 100755 index 0000000..2a9ed4c Binary files /dev/null and b/AmzPhoto/AmzPhoto/PagePhotos/1933_2.jpg differ diff --git a/AmzPhoto/AmzPhoto/PagePhotos/1933_3.jpg b/AmzPhoto/AmzPhoto/PagePhotos/1933_3.jpg new file mode 100755 index 0000000..9661bd6 Binary files /dev/null and b/AmzPhoto/AmzPhoto/PagePhotos/1933_3.jpg differ diff --git a/AmzPhoto/AmzPhoto/PagePhotos/1933_4.jpg b/AmzPhoto/AmzPhoto/PagePhotos/1933_4.jpg new file mode 100755 index 0000000..c19f2db Binary files /dev/null and b/AmzPhoto/AmzPhoto/PagePhotos/1933_4.jpg differ diff --git a/AmzPhoto/AmzPhoto/PagePhotos/1933_5.jpg b/AmzPhoto/AmzPhoto/PagePhotos/1933_5.jpg new file mode 100755 index 0000000..9ff95d8 Binary files /dev/null and b/AmzPhoto/AmzPhoto/PagePhotos/1933_5.jpg differ diff --git a/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosDataSource.h b/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosDataSource.h new file mode 100644 index 0000000..3a94897 --- /dev/null +++ b/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosDataSource.h @@ -0,0 +1,22 @@ +// +// PagePhotosDataSource.h +// PagePhotosDemo +// +// Created by junmin liu on 10-8-23. +// Copyright 2010 Openlab. All rights reserved. +// + +#import + + +@protocol PagePhotosDataSource + +// 有多少页 +// +- (int)numberOfPages; + +// 每页的图片 +// +- (UIImage *)imageAtIndex:(int)index; + +@end diff --git a/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosView.h b/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosView.h new file mode 100644 index 0000000..63b848d --- /dev/null +++ b/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosView.h @@ -0,0 +1,32 @@ +// +// PagePhotosView.h +// PagePhotosDemo +// +// Created by junmin liu on 10-8-23. +// Copyright 2010 Openlab. All rights reserved. +// + +#import +#import "PagePhotosDataSource.h" + +#define kPageScrollDone @"kPageScrollDone" + +@interface PagePhotosView : UIView { + UIScrollView *scrollView; + UIPageControl *pageControl; + + id dataSource; + NSMutableArray *imageViews; + + // To be used when scrolls originate from the UIPageControl + BOOL pageControlUsed; +} + +@property (nonatomic, assign) id dataSource; +@property (nonatomic, retain) NSMutableArray *imageViews; + +- (IBAction)changePage:(id)sender; + +- (id)initWithFrame:(CGRect)frame withDataSource:(id)_dataSource; + +@end diff --git a/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosView.m b/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosView.m new file mode 100644 index 0000000..3cc405f --- /dev/null +++ b/AmzPhoto/AmzPhoto/PagePhotos/PagePhotosView.m @@ -0,0 +1,157 @@ +// +// PagePhotosView.m +// PagePhotosDemo +// +// Created by junmin liu on 10-8-23. +// Copyright 2010 Openlab. All rights reserved. +// + +#import "PagePhotosView.h" + +@interface PagePhotosView (PrivateMethods) + +- (void)loadScrollViewWithPage:(int)page; +- (void)scrollViewDidScroll:(UIScrollView *)sender; + +@end + +@implementation PagePhotosView +@synthesize dataSource; +@synthesize imageViews; + +- (id)initWithFrame:(CGRect)frame withDataSource:(id)_dataSource { + if ((self = [super initWithFrame:frame])) { + self.dataSource = _dataSource; + // Initialization UIScrollView + int pageControlHeight = 20; + scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - pageControlHeight)]; + pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - pageControlHeight, frame.size.width, pageControlHeight)]; + + [self addSubview:scrollView]; + [self addSubview:pageControl]; + + int kNumberOfPages = [dataSource numberOfPages]; + + // in the meantime, load the array with placeholders which will be replaced on demand + NSMutableArray *views = [[NSMutableArray alloc] init]; + for (unsigned i = 0; i < kNumberOfPages; i++) { + [views addObject:[NSNull null]]; + } + self.imageViews = views; + [views release]; + + // a page is the width of the scroll view + scrollView.pagingEnabled = YES; + scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); + scrollView.showsHorizontalScrollIndicator = NO; + scrollView.showsVerticalScrollIndicator = NO; + scrollView.scrollsToTop = NO; + scrollView.delegate = self; + + pageControl.numberOfPages = kNumberOfPages; + pageControl.currentPage = 0; + pageControl.backgroundColor = [UIColor blackColor]; + + // 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]; + + } + return self; +} + + +- (void)loadScrollViewWithPage:(int)page { + int kNumberOfPages = [dataSource numberOfPages]; + + if (page < 0) return; + if (page >= kNumberOfPages) return; + + // replace the placeholder if necessary + UIImageView *view = [imageViews objectAtIndex:page]; + if ((NSNull *)view == [NSNull null]) { + UIImage *image = [dataSource imageAtIndex:page]; + view = [[UIImageView alloc] initWithImage:image]; + [imageViews replaceObjectAtIndex:page withObject:view]; + [view release]; + } + + // add the controller's view to the scroll view + if (nil == view.superview) { + CGRect frame = scrollView.frame; + frame.origin.x = frame.size.width * page; + frame.origin.y = 0; + view.frame = frame; + [scrollView addSubview: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 = scrollView.frame.size.width; + int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; + //time's up to quit + if(pageControl.currentPage==[dataSource numberOfPages]-1) + { + [[NSNotificationCenter defaultCenter]postNotificationName:kPageScrollDone object:nil]; + return; + } + 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; +} + +- (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 = scrollView.frame; + frame.origin.x = frame.size.width * page; + frame.origin.y = 0; + [scrollView scrollRectToVisible:frame animated:YES]; + + // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above. + pageControlUsed = YES; +} + + +- (void)dealloc { + [scrollView release]; + [pageControl release]; + [super dealloc]; +} + + +@end