Skip to content

Instantly share code, notes, and snippets.

@curt-labs
Created April 13, 2011 15:19
Show Gist options
  • Save curt-labs/917730 to your computer and use it in GitHub Desktop.
Save curt-labs/917730 to your computer and use it in GitHub Desktop.
Scroll view not showing horizontal indicator
//init product controller replace view with new view and product controller
ProductScrollView *mvc = [[ProductScrollView alloc] init];
// set frame of scrollview
mvc.scrollView.frame = CGRectMake(0, 125, firstView.bounds.size.width, (firstView.bounds.size.height-125));
//mvc.scrollView.bounds = firstView.bounds;
[firstView addSubview:mvc.scrollView];
[firstView release];
@interface ProductScrollView : ContentController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
Vehicle *sharedVehicle;
VehiclePicker *sharedPicker;
NSMutableArray *productArray;
NSMutableArray *viewControllers;
BOOL arrayPopulated;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) Vehicle *sharedVehicle;
@property (nonatomic, retain) VehiclePicker *sharedPicker;
@property (nonatomic, retain) NSMutableArray *productArray;
@property (nonatomic, retain) NSMutableArray *viewControllers;
-(void)buildProducts;
@end
@interface ContentController (PrivateMethods)
-(void) loadScrollViewWithPage:(int)page;
-(void)scrollViewDidScroll:(UIScrollView *)sender;
@end
@implementation ProductScrollView
@synthesize scrollView, sharedPicker, sharedVehicle, productArray, viewControllers;
-(id)init{
[super init];
//Post notification to default center for asynchronous call finished
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable) name:@"getHitch" object:nil];
if(scrollView == nil){
scrollView = [[UIScrollView alloc] init];
}
self.sharedPicker = [VehiclePicker sharedPicker];
self.sharedVehicle = [Vehicle sharedVehicle];
if (picUrls == nil) {
picUrls = [NSMutableDictionary new];
}
arrayPopulated = NO;
//Call data method pass current data and populate dataArray
[self.sharedPicker getHitch:[self.sharedVehicle vchYear]:[self.sharedVehicle vchMake]:[self.sharedVehicle vchModel]:[self.sharedVehicle vchStyle]];
//Initialize internal dataArray with data set
if ([self.productArray count] == 0) {
self.productArray = [NSMutableArray arrayWithArray:[self.sharedPicker dataArray]];
}
[self buildProducts];
return self;
}
-(void)buildProducts{
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i=([self.productArray count]); i > 0; i--) {
[controllers insertObject:[NSNull null] atIndex:0];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.productArray count], scrollView.frame.size.height);
scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
scrollView.scrollsToTop = NO;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.delegate = self;
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (UIView *)view
{
return self.scrollView;
}
- (void)dealloc
{
[viewControllers release];
[scrollView release];
[super dealloc];
}
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= [self.productArray count])
return;
// replace the placeholder if necessary
ProductViewController *controller = [viewControllers objectAtIndex:page];
Product *currProd = [self.productArray objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[ProductViewController alloc] initWithPageNumber:page:currProd];
[currProd release];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// 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;
// 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
}
-(void)reloadTable
{
self.productArray = [[NSMutableArray alloc] initWithArray:sharedPicker.dataArray];
arrayPopulated = YES;
[self buildProducts];
}
@interface ProductViewController : UIViewController {
int pageNumber;
Product *intProd;
UIPopoverController *popController;
}
-(id)initWithPageNumber:(int)page:(Product *)prod;
-(IBAction)zoom:(id)sender;
-(IBAction)orderNow:(id)sender;
@implementation ProductViewController
-(id)initWithPageNumber:(int)page :(Product *)prod{
if (self) {
pageNumber = page;
ProductView *prodView = (ProductView *)self.view;
[prodView setProduct:prod];
intProd = prod;
}
return self;
}
-(void)zoom:(id)sender{
ProductView *prodView = (ProductView *)self.view; //Reference to view
//create close button
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closePopup)] autorelease];
//image from the product
UIImageView *imageView = [[UIImageView alloc] initWithImage:[[prodView prodImage] backgroundImageForState:UIControlStateNormal]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.contentScaleFactor = .8;
//view Controller for image in popup
UIViewController *imageViewController = [[UIViewController new] autorelease];
[imageViewController setTitle:[prodView.titleLabel text]]; //button title
imageViewController.navigationItem.leftBarButtonItem = closeButton; //add button to controller
imageViewController.contentSizeForViewInPopover = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
//add image to view controller
[imageViewController.view addSubview:imageView];
[imageView release];
//create nav controller for button
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:imageViewController] autorelease];
//create popup controller init with nav controller
popController = [[UIPopoverController alloc] initWithContentViewController:nc];
//make visible or dismiss and release mem
if (![popController isPopoverVisible]) {
[popController presentPopoverFromRect:CGRectMake(0, 0, 0, 0) inView:self.view.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}else{
[popController dismissPopoverAnimated:YES];
[popController release];
}
}
-(void)orderNow:(id)sender{
}
-(void)closePopup{
[popController dismissPopoverAnimated:YES];
[popController release];
}
- (void)dealloc
{
if ([popController isPopoverVisible]) {
[popController dismissPopoverAnimated:YES];
[popController release];
}
[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.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment