PunchScrollView – the iOS ScrollView Framework which works like UITableView

PunchScrollView hosted on github is a ScrollView framework for iOS (iPhone, iPad and iPod Touch) which works like the Apple UITableView
[Update from WWDC 2012] Apple introduced the UICollectionViewController in iOS 6 which has the same purpose as PunchScrollView. Because it does the same things I will stop developing PunchScrollView in the near future.[/Update]

  • Easy and fast implementation, almost like the UITableView
  • Perfect for photo slideshows or many text pages
  • Use the benefits of the NSIndexPath like you might know it from the UITableView
  • Easy setup in combination with Core Data
  • Helpful methods, i.e. jump or scroll to a desired page
  • Avoid boilerplate code
  • Save lots of memory with the use of dequeuing

The following code shows a simple scrollview with 3 sections and 3 pages

- (NSInteger)numberOfSectionsInPunchScrollView:(PunchScrollView *)scrollView
{
	return 3;
}

- (NSInteger)punchscrollView:(PunchScrollView *)scrollView numberOfPagesInSection:(NSInteger)section
{
	return 3;
}

- (UIView*)punchScrollView:(PunchScrollView*)scrollView viewForPageAtIndexPath:(NSIndexPath *)indexPath
{
	ExamplePageView *page = (ExamplePageView*)[scrollView dequeueRecycledPage];
	if (page == nil)
	{
		page = [[[ExamplePageView alloc] initWithFrame:self.view.bounds] autorelease];
	}

	page.titleLabel.text = [NSString stringWithFormat:@"Page %d in section %d", indexPath.row, indexPath.section];

	return page;
}

Recently I added UIViewController support to PunchScrollView. Just return a UIViewController and PunchScrollView will handle the caching and lazy loading itself
The following example demonstrates a simple usage with UIViewController

- (NSInteger)punchscrollView:(PunchScrollView *)scrollView numberOfPagesInSection:(NSInteger)section
{
	return 3;
}

- (UIViewController*)punchScrollView:(PunchScrollView *)scrollView controllerForPageAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.childViewControllers objectAtIndex:indexPath.row];
}

For those folks, who are interested in the details, here we go with the properties, methods, delegate & datasource methods

or just go directly to github to clone PunchScrollView

Properties

Set the DataSource for PunchScrollView

id dataSource;

Set the Delegate for PunchScrollView

id delegate;

Set the padding between pages . Default is 10pt

CGFloat pagePadding;

Set a Vertical or Horizontal Direction of the scrolling

PunchScrollViewDirection direction;

Get the current visible Page

UIView *currentPage;

Get the first Page

UIView *firstPage;

Get the last Page

UIView *lastPage;

Get the current visible indexPath

NSIndexPath *currentIndexPath;

Get the last indexPath of the Scroll Suite

NSIndexPath *lastIndexPath;

Returns all UIViewController hold by the PunchScrollView

 NSArray *pageController;  

Initializer

- (id)init;
- (id)initWithFrame:(CGRect)aFrame;

Methods

Using recycled Pages, instead of allocating new pages
This Method returns a UIView which is in the Queue

- (UIView *)dequeueRecycledPage;

Refresh the ScrollView and its pages

- (void)reloadData;

This Method returns an UIView for a given indexPath

- (UIView*)pageForIndexPath:(NSIndexPath*)indexPath;

Scroll to a specified indexPath, can be animated, if set to YES

- (void)scrollToIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;

Scroll to the next page, can be animated, if set to YES

- (void)scrollToNextPage:(BOOL)animated;

Scroll to the previous page, can be animated, if set to YES

- (void)scrollToPreviousPage:(BOOL)animated;

PunchScrollView Delegate Methods

Will be called if the scrollview changes the page

- (void)punchScrollView:(PunchScrollView*)scrollView pageChanged:(NSIndexPath*)indexPath;

Called when the page will be unloaded. We used to call “viewDidUnload” – but this method will be deprecated in iOS 6
Please destroy all your views in that delegate call for the controller. Otherwise your app will leak!!

- (void)punchScrollView:(PunchScrollView*)scrollView
             unloadPage:(UIView*)view
           forController:(UIViewController*)controller;

PunchScrollView DataSource Methods

Set the number of sections (optional, default is 1, if not implemented)

- (NSInteger)numberOfSectionsInPunchScrollView:(PunchScrollView *)scrollView;

Set the number of pages in the specified section

- (NSInteger)punchscrollView:(PunchScrollView *)scrollView numberOfPagesInSection:(NSInteger)section;

Return the page for a specified indexPath

- (UIView*)punchScrollView:(PunchScrollView*)scrollView viewForPageAtIndexPath:(NSIndexPath *)indexPath;

Return a UIViewController for the indexPath (optional)

- (UIViewController*)punchScrollView:(PunchScrollView*)scrollView controllerForPageAtIndexPath:(NSIndexPath *)indexPath;

Set the number of pages to be loaded lazy and kept in the cache (optional), Default is 0

- (NSInteger)numberOfLazyLoadingPages;

9 thoughts on “PunchScrollView – the iOS ScrollView Framework which works like UITableView

  1. Thanks for your reply. Right now are busy. Maybe we add the support later this year.

  2. Some what confused about adding core data to this? would it use a fetchedResultsController like a table view (PunchUIScrollViewViewController.m -> self.table)? or would the data be connections core data -> data source (PunchScrollView.m)?

    thank you for this.

  3. can it use with ARC? If i have tabbarcontroller, can i use it with punchscrollview?

  4. Yes you can use it with ARC
    but you must exclude it with -fno-objc-arc
    in “Build Phases/Compile Sources” in your Project settings

  5. I have this code :
    TopSongsVC *topSongsVC = [[TopSongsVC alloc] init];
    UINavigationController *navi_topSongs = [[UINavigationController alloc] initWithRootViewController:topSongsVC];

    UITabBarController *tabbarController = [[UITabBarController alloc] init];
    tabbarController.viewControllers = @[navi_topSongs];

    self.window.rootViewController = tabbarController;

    with TopSongsVC is a UIViewController. What must i do to display TopSongsVC like as PunchViewController. I use XCode 4.5

  6. That looks fine
    You just build an instance of PunchscrollView in your TopSongsVC
    like in the example PunchUIScrollViewViewController

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>