For the application I'm working on, I need a scrollview with paging along with several views (in this case tableviews). Setting this option (using the Apple PageControl example) is quite simple, and I have no problem working with it regarding scrolling and linking between pages. I want tableviews to be almost similar to thumbnail views of table views in scrollview. As soon as you click one, it will open or zoom in so that the table screen spans the entire screen. Again, tweaking this question is not really a problem, I can make the views inside scrollview any size I want and click, I'm sure I can work when I get to this part. :)
My problem is this: in any given view, no matter what page you are on, I want the next page and previous page to be slightly visible on the right and left, respectively. Of course, the first page will only have one page shown on the right side, or the left side for the last page. Again, by setting the x-origin of the views (tabular views), I can get the first page to look right. However, as soon as you view the problem, it becomes obvious, namely, that the screen scrolls too far.
Breaking it, I think my main problem is that I cannot change the amount of scroll that is performed when switching pages.
I would appreciate any help I can get, thanks.
Appdelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
UIWindow *window;
UIView *background;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UIView *background;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
@property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)changePage:(id)sender;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "MyViewController.h"
static NSUInteger kNumberOfPages = 4;
@interface AppDelegate (PrivateMethods)
- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;
@end
@implementation AppDelegate
@synthesize window, scrollView, pageControl, viewControllers, background;
- (void)dealloc {
[background release];
[viewControllers release];
[scrollView release];
[pageControl release];
[window release];
[super dealloc];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
background.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
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;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = CGRectMake(50, 50, 220, 330);
frame.origin.x = 50 + (250 * page);
frame.origin.y = 50;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
}
@end