Skip to content

Instantly share code, notes, and snippets.

@andreacremaschi
Last active August 29, 2015 14:02
Show Gist options
  • Save andreacremaschi/0aeeea12fa66ca1fbf78 to your computer and use it in GitHub Desktop.
Save andreacremaschi/0aeeea12fa66ca1fbf78 to your computer and use it in GitHub Desktop.
ACPageViewController
A PageViewController subclass with a configuration block and scroll handling.
//
// ACPageViewController.h
//
// Created by Andrea Cremaschi on 09/04/14.
// Copyright (c) 2014 midapp. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef UIViewController*(^PageViewControllerConfigurationBlock)(int pageNumber);
@interface ACPageViewController : UIPageViewController
@property (strong) PageViewControllerConfigurationBlock pageViewControllerConfigurationBlock;
@property int currentPage;
@end
//
// PageViewController.m
// duczogno
//
// Created by Andrea Cremaschi on 09/04/14.
// Copyright (c) 2014 midapp. All rights reserved.
//
#import "ACPageViewController.h"
#import "UIViewController+PageViewController.h"
@interface ACPageViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate>
@end
@implementation ACPageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(UIScrollView*)scrollView {
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
return (UIScrollView*)view;
}
}
return nil;
}
- (UIViewController*)viewControllerAtIndex: (int) index {
UIViewController *pageVC;
if (self.pageViewControllerConfigurationBlock) {
pageVC = self.pageViewControllerConfigurationBlock(index);
// TODO: if the pageVC.view's contains a scrollview, its delegate must be setted to self
// schedaVC.scrollViewDelegate= self;
} else {
pageVC = [UIViewController new];
}
pageVC.pageIndex = index;
return pageVC;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// imposta il delegato della scrollview
[[self scrollView] setDelegate:self];
// Elimina la tap gesture recognizer perché interferisce con lo scroll della pagina
UIGestureRecognizer* tapRecognizer = nil;
for (UIGestureRecognizer* recognizer in self.gestureRecognizers) {
if ( [recognizer isKindOfClass:[UITapGestureRecognizer class]] ) {
tapRecognizer = recognizer;
break;
}
}
if ( tapRecognizer ) {
[self.view removeGestureRecognizer:tapRecognizer];
}
self.dataSource = self;
self.delegate = self;
UIViewController *vc = [self viewControllerAtIndex: self.currentPage];
[self setViewControllers: @[vc]
direction: UIPageViewControllerNavigationDirectionForward
animated: YES
completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIPageViewControllerDatasource
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
int curIndex = viewController.pageIndex;
return [self viewControllerAtIndex:curIndex +1];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
int curIndex = viewController.pageIndex;
if (curIndex == 0) return nil;
return [self viewControllerAtIndex:curIndex -1];
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (scrollView != [self scrollView]) {
[[self scrollView] setScrollEnabled: NO];
} else {
UIScrollView *childScrollView = (UIScrollView*)[self.viewControllers.firstObject view];
childScrollView.scrollEnabled = NO;
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[[self scrollView] setScrollEnabled: YES];
UIScrollView *childScrollView = (UIScrollView*)[self.viewControllers.firstObject view];
childScrollView.scrollEnabled = YES;
}
@end
Pod::Spec.new do |s|
s.name = "ACPageViewController"
s.version = "0.0.1"
s.summary = "A PageViewController subclass with a configuration block and scroll handling."
s.description = <<-DESC
DESC
s.homepage = "https://gist.github.com/andreacremaschi/0aeeea12fa66ca1fbf78"
# s.license = "MIT"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Andrea Cremaschi" => "andrea.cremaschi@midainformatica.it" }
s.platform = :ios, "5.0"
s.source = { :git => "https://gist.github.com/0aeeea12fa66ca1fbf78.git", :tag => "0.0.1" }
s.source_files = "*.{h,m}"
s.public_header_files = "ACPageViewController.h"
s.requires_arc = true
end
The MIT License (MIT)
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
//
// UIViewController+PageViewController.h
// duczogno
//
// Created by Andrea Cremaschi on 09/04/14.
// Copyright (c) 2014 midapp. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIViewController (PageViewController)
@property int pageIndex;
@end
//
// UIViewController+PageViewController.m
// duczogno
//
// Created by Andrea Cremaschi on 09/04/14.
// Copyright (c) 2014 midapp. All rights reserved.
//
#import "UIViewController+PageViewController.h"
#import <objc/runtime.h>
static char kViewControllerIndexKey;
@implementation UIViewController (PageViewController)
- (int)pageIndex {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
NSNumber* pageIndexNumber = objc_getAssociatedObject(self, &kViewControllerIndexKey);
#pragma clang diagnostic pop
return pageIndexNumber.intValue;
}
- (void)setPageIndex:(int)imageCache {
objc_setAssociatedObject(self, &kViewControllerIndexKey, @(imageCache), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment