我在一个ViewController中有3个ViewControllers
ViewPagerController = self - >它是一个viewController并具有UIPageViewController init
ProgramQuestionViewController =它基于ViewPagerController
QuestionViewController =我的内容视图显示在ProgramViewController
的中间这是我的ViewPagerController
@interface ViewPagerController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
NSMutableArray *dataAnswers;
}
@property UIScrollView *tabsView;
@property UIView *contentView;
@property UIPageViewController *pageViewController;
@property (assign) id<UIScrollViewDelegate> actualDelegate;
@property (assign)bool isValidPage;
@property (assign)bool isSonraki;
// Tab and content cache
@property NSMutableArray *tabs;
@property NSMutableArray *contents;
// Options
@property (nonatomic) NSNumber *tabHeight;
@property (nonatomic) NSNumber *tabOffset;
@property (nonatomic) NSNumber *tabWidth;
@property (nonatomic) NSNumber *tabLocation;
@property (nonatomic) NSNumber *startFromSecondTab;
@property (nonatomic) NSNumber *centerCurrentTab;
@property (nonatomic) NSNumber *fixFormerTabsPositions;
@property (nonatomic) NSNumber *fixLatterTabsPositions;
@property (nonatomic,retain)UIButton *backButton;
@property (nonatomic,retain)UIButton *nextButton;
@property (nonatomic) NSUInteger tabCount;
@property (nonatomic) NSUInteger activeTabIndex;
@property (nonatomic) NSUInteger activeContentIndex;
@property (getter = isAnimatingToTab, assign) BOOL animatingToTab;
@property (getter = isDefaultSetupDone, assign) BOOL defaultSetupDone;
@property (nonatomic) UIColor *indicatorColor;
@property (nonatomic) UIColor *tabsViewBackgroundColor;
@property (nonatomic) UIColor *contentViewBackgroundColor;
@end
@implementation ViewPagerController
@synthesize tabHeight = _tabHeight;
@synthesize tabOffset = _tabOffset;
@synthesize tabWidth = _tabWidth;
@synthesize tabLocation = _tabLocation;
@synthesize startFromSecondTab = _startFromSecondTab;
@synthesize centerCurrentTab = _centerCurrentTab;
@synthesize fixFormerTabsPositions = _fixFormerTabsPositions;
@synthesize fixLatterTabsPositions = _fixLatterTabsPositions;
@synthesize isValidPage;
@synthesize backButton;
@synthesize nextButton;
@synthesize isSonraki;
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self defaultSettings];
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self defaultSettings];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
isSonraki=false;
isValidPage=true;
dataAnswers =[[NSMutableArray alloc] init];
UIImageView *pagerBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"splash_bg.png"]];
pagerBackground.frame=CGRectMake(0, 800, self.view.frame.size.width, 160);
[self.view setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:0.75]];
backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(50, 875, 87, 45)];
[backButton setBackgroundImage:[UIImage imageNamed:@"prev_btn.png"] forState:UIControlStateNormal];
//UITapGestureRecognizer *tapGestureRecognizerBack = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackTapGesture:)];
//[backButton addGestureRecognizer:tapGestureRecognizerBack];
[backButton addTarget:self action:@selector(handleBackTapGesture:) forControlEvents:UIControlEventTouchUpInside];
backButton.hidden=YES;
nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nextButton setFrame:CGRectMake(620, 875, 87, 45)];
[nextButton setBackgroundImage:[UIImage imageNamed:@"next_btn.png"] forState:UIControlStateNormal];
//UITapGestureRecognizer *tapGestureRecognizerNext = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleNextTapGesture:)];
//[nextButton addGestureRecognizer:tapGestureRecognizerNext];
[nextButton addTarget:self action:@selector(handleNextTapGesture:) forControlEvents:UIControlEventTouchUpInside];
for (UIGestureRecognizer *gr in [self.pageViewController gestureRecognizers]) {
[gr setEnabled:NO];
}
[self.view addSubview:nextButton];
[self.view addSubview:backButton];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//Touch gestures below top bar should not make the page turn.
//EDITED Check for only Tap here instead.
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
CGPoint touchPoint = [touch locationInView:self.view];
if (touchPoint.y > 40) {
return NO;
}
else if (touchPoint.x > 50 && touchPoint.x < 430) {//Let the buttons in the middle of the top bar receive the touch
return NO;
}
}
return YES;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Do setup if it's not done yet
if (![self isDefaultSetupDone]) {
[self defaultSetup];
NSLog(@"hello");
}
}
- (void)viewWillLayoutSubviews {
// Re-layout sub views
[self layoutSubviews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)layoutSubviews {
CGFloat topLayoutGuide = 0.0;
CGRect frame = self.tabsView.frame;
frame.origin.x = 150.0;
frame.origin.y = [self.tabLocation boolValue] ? topLayoutGuide : CGRectGetHeight(self.view.frame) - [self.tabHeight floatValue];
frame.size.width = 450;//CGRectGetWidth(self.view.frame);
frame.size.height = [self.tabHeight floatValue];
self.tabsView.frame = frame;
frame = self.contentView.frame;
frame.origin.x = 0.0;
frame.origin.y = [self.tabLocation boolValue] ? topLayoutGuide + CGRectGetHeight(self.tabsView.frame) : topLayoutGuide;
frame.size.width = CGRectGetWidth(self.view.frame);
frame.size.height = CGRectGetHeight(self.view.frame) - (topLayoutGuide + CGRectGetHeight(self.tabsView.frame)) - CGRectGetHeight(self.tabBarController.tabBar.frame);
self.contentView.frame = frame;
}
- (IBAction)handleTapGesture:(id)sender {
}
- (IBAction)handleNextTapGesture:(id)sender {
//UIView *myvview=[self.dataSource viewPager:self contentViewForTabAtIndex:0];
//backButton.hidden=NO;
UIViewController *viewController = [self.dataSource viewPager:self contentViewControllerForTabAtIndex:_activeContentIndex];
UIViewController *xvc=[LoggedinUser sharedCenter].myQuestionView;
[self saveMyData:xvc];
if (isValidPage) {
[self selectTabAtIndex:self.activeContentIndex+1];
}
}
- (IBAction)handleBackTapGesture:(id)sender {
if(self.activeTabIndex==0)
{
backButton.hidden=YES;
}
else
{
backButton.hidden=NO;
}
UIViewController *viewController = [self.dataSource viewPager:self contentViewControllerForTabAtIndex:_activeContentIndex];
UIViewController *xvc=[LoggedinUser sharedCenter].myQuestionView;
[self saveMyData:xvc];
if (isValidPage) {
[self selectTabAtIndex:self.activeContentIndex-1];
}
}
- (void)setActiveTabIndex:(NSUInteger)activeTabIndex {
TabView *activeTabView;
// Set to-be-inactive tab unselected
activeTabView = [self tabViewAtIndex:self.activeTabIndex];
activeTabView.selected = NO;
// Set to-be-active tab selected
activeTabView = [self tabViewAtIndex:activeTabIndex];
activeTabView.selected = YES;
// Set current activeTabIndex
_activeTabIndex = activeTabIndex;
// Bring tab to active position
// Position the tab in center if centerCurrentTab option is provided as YES
UIView *tabView = [self tabViewAtIndex:self.activeTabIndex];
CGRect frame = tabView.frame;
if ([self.centerCurrentTab boolValue]) {
frame.origin.x += (CGRectGetWidth(frame) / 2);
frame.origin.x -= CGRectGetWidth(self.tabsView.frame) / 2;
frame.size.width = CGRectGetWidth(self.tabsView.frame);
if (frame.origin.x < 0) {
frame.origin.x = 0;
}
if ((frame.origin.x + CGRectGetWidth(frame)) > self.tabsView.contentSize.width) {
frame.origin.x = (self.tabsView.contentSize.width - CGRectGetWidth(self.tabsView.frame));
}
} else {
//frame.origin.x -= [self.tabOffset floatValue];
frame.origin.x -= [self.tabOffset floatValue];
frame.size.width = CGRectGetWidth(self.tabsView.frame);
}
[self.tabsView scrollRectToVisible:frame animated:YES];
}
- (void)setActiveContentIndex:(NSUInteger)activeContentIndex {
// Get the desired viewController
UIViewController *viewController = [self viewControllerAtIndex:activeContentIndex];
if (!viewController) {
viewController = [[UIViewController alloc] init];
viewController.view = [[UIView alloc] init];
viewController.view.backgroundColor = [UIColor clearColor];
}
// __weak pageViewController to be used in blocks to prevent retaining strong reference to self
__weak UIPageViewController *weakPageViewController = self.pageViewController;
__weak ViewPagerController *weakSelf = self;
if (activeContentIndex == self.activeContentIndex) {
[self.pageViewController setViewControllers:@[viewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^(BOOL completed) {
weakSelf.animatingToTab = NO;
}];
} else if (!(activeContentIndex + 1 == self.activeContentIndex || activeContentIndex - 1 == self.activeContentIndex)) {
[self.pageViewController setViewControllers:@[viewController]
direction:(activeContentIndex < self.activeContentIndex) ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward
animated:YES
completion:^(BOOL completed) {
weakSelf.animatingToTab = NO;
// Set the current page again to obtain synchronisation between tabs and content
dispatch_async(dispatch_get_main_queue(), ^{
[weakPageViewController setViewControllers:@[viewController]
direction:(activeContentIndex < weakSelf.activeContentIndex) ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
});
}];
} else {
[self.pageViewController setViewControllers:@[viewController]
direction:(activeContentIndex < self.activeContentIndex) ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward
animated:YES
completion:^(BOOL completed) {
weakSelf.animatingToTab = NO;
}];
}
// Clean out of sight contents
NSInteger index;
index = self.activeContentIndex - 1;
if (index >= 0 &&
index != activeContentIndex &&
index != activeContentIndex - 1)
{
[self.contents replaceObjectAtIndex:index withObject:[NSNull null]];
}
index = self.activeContentIndex;
if (index != activeContentIndex - 1 &&
index != activeContentIndex &&
index != activeContentIndex + 1)
{
[self.contents replaceObjectAtIndex:index withObject:[NSNull null]];
}
index = self.activeContentIndex + 1;
if (index < self.contents.count &&
index != activeContentIndex &&
index != activeContentIndex + 1)
{
[self.contents replaceObjectAtIndex:index withObject:[NSNull null]];
}
_activeContentIndex = activeContentIndex;
}
- (void)reloadData {
// Empty all options and colors
// So that, ViewPager will reflect the changes
// Empty all options
_tabHeight = nil;
_tabOffset = nil;
_tabWidth = nil;
_tabLocation = nil;
_startFromSecondTab = nil;
_centerCurrentTab = nil;
_fixFormerTabsPositions = nil;
_fixLatterTabsPositions = nil;
// Empty all colors
_indicatorColor = nil;
_tabsViewBackgroundColor = nil;
_contentViewBackgroundColor = nil;
// Call to setup again with the updated data
[self defaultSetup];
}
- (void)selectTabAtIndex:(NSUInteger)index {
if (index >= self.tabCount) {
return;
}
self.animatingToTab = YES;
// Set activeTabIndex
self.activeTabIndex = index;
// Set activeContentIndex
self.activeContentIndex = index;
// Inform delegate about the change
if ([self.delegate respondsToSelector:@selector(viewPager:didChangeTabToIndex:)]) {
[self.delegate viewPager:self didChangeTabToIndex:self.activeTabIndex];
}
}
- (void)setNeedsReloadOptions {
// If our delegate doesn't respond to our options method, return
// Otherwise reload options
if (![self.delegate respondsToSelector:@selector(viewPager:valueForOption:withDefault:)]) {
return;
}
// Update these options
self.tabWidth = [NSNumber numberWithFloat:[self.delegate viewPager:self valueForOption:ViewPagerOptionTabWidth withDefault:kTabWidth]];
self.tabOffset = [NSNumber numberWithFloat:[self.delegate viewPager:self valueForOption:ViewPagerOptionTabOffset withDefault:kTabOffset]];
self.centerCurrentTab = [NSNumber numberWithFloat:[self.delegate viewPager:self valueForOption:ViewPagerOptionCenterCurrentTab withDefault:kCenterCurrentTab]];
self.fixFormerTabsPositions = [NSNumber numberWithFloat:[self.delegate viewPager:self valueForOption:ViewPagerOptionFixFormerTabsPositions withDefault:kFixFormerTabsPositions]];
self.fixLatterTabsPositions = [NSNumber numberWithFloat:[self.delegate viewPager:self valueForOption:ViewPagerOptionFixLatterTabsPositions withDefault:kFixLatterTabsPositions]];
// We should update contentSize property of our tabsView, so we should recalculate it with the new values
CGFloat contentSizeWidth = 0;
// Give the standard offset if fixFormerTabsPositions is provided as YES
if ([self.fixFormerTabsPositions boolValue]) {
// And if the centerCurrentTab is provided as YES fine tune the offset according to it
if ([self.centerCurrentTab boolValue]) {
contentSizeWidth = (CGRectGetWidth(self.tabsView.frame) - [self.tabWidth floatValue]) / 2.0;
} else {
contentSizeWidth = [self.tabOffset floatValue];
}
}
// Update every tab's frame
for (NSUInteger i = 0; i < self.tabCount; i++) {
UIView *tabView = [self tabViewAtIndex:i];
CGRect frame = tabView.frame;
frame.origin.x = contentSizeWidth;
frame.size.width = [self.tabWidth floatValue];
tabView.frame = frame;
contentSizeWidth += CGRectGetWidth(tabView.frame);
}
// Extend contentSizeWidth if fixLatterTabsPositions is provided YES
if ([self.fixLatterTabsPositions boolValue]) {
// And if the centerCurrentTab is provided as YES fine tune the content size according to it
if ([self.centerCurrentTab boolValue]) {
contentSizeWidth += (CGRectGetWidth(self.tabsView.frame) - [self.tabWidth floatValue]) / 2.0;
} else {
contentSizeWidth += CGRectGetWidth(self.tabsView.frame) - [self.tabWidth floatValue] - [self.tabOffset floatValue];
}
}
// Update tabsView's contentSize with the new width
self.tabsView.contentSize = CGSizeMake(contentSizeWidth, [self.tabHeight floatValue]);
}
- (void)setNeedsReloadColors {
// If our delegate doesn't respond to our colors method, return
// Otherwise reload colors
if (![self.delegate respondsToSelector:@selector(viewPager:colorForComponent:withDefault:)]) {
return;
}
// These colors will be updated
UIColor *indicatorColor;
UIColor *tabsViewBackgroundColor;
UIColor *contentViewBackgroundColor;
// Get indicatorColor and check if it is different from the current one
// If it is, update it
indicatorColor = [self.delegate viewPager:self colorForComponent:ViewPagerIndicator withDefault:kIndicatorColor];
if (![self.indicatorColor isEqualToColor:indicatorColor]) {
// We will iterate through all of the tabs to update its indicatorColor
[self.tabs enumerateObjectsUsingBlock:^(TabView *tabView, NSUInteger index, BOOL *stop) {
tabView.indicatorColor = indicatorColor;
}];
// Update indicatorColor to check again later
self.indicatorColor = indicatorColor;
}
// Get tabsViewBackgroundColor and check if it is different from the current one
// If it is, update it
tabsViewBackgroundColor = [self.delegate viewPager:self colorForComponent:ViewPagerTabsView withDefault:kTabsViewBackgroundColor];
if (![self.tabsViewBackgroundColor isEqualToColor:tabsViewBackgroundColor]) {
// Update it
self.tabsView.backgroundColor = tabsViewBackgroundColor;
// Update tabsViewBackgroundColor to check again later
self.tabsViewBackgroundColor = tabsViewBackgroundColor;
}
// Get contentViewBackgroundColor and check if it is different from the current one
// Yeah update it, too
//contentViewBackgroundColor = [self.delegate viewPager:self colorForComponent:ViewPagerContent withDefault:kContentViewBackgroundColor];
//if (![self.contentViewBackgroundColor isEqualToColor:contentViewBackgroundColor]) {
// Yup, update
// self.contentView.backgroundColor = contentViewBackgroundColor;
// Update this, too, to check again later
// self.contentViewBackgroundColor = contentViewBackgroundColor;
//}
}
- (CGFloat)valueForOption:(ViewPagerOption)option {
switch (option) {
case ViewPagerOptionTabHeight:
return [[self tabHeight] floatValue];
case ViewPagerOptionTabOffset:
return [[self tabOffset] floatValue];
case ViewPagerOptionTabWidth:
return [[self tabWidth] floatValue];
case ViewPagerOptionTabLocation:
return [[self tabLocation] floatValue];
case ViewPagerOptionStartFromSecondTab:
return [[self startFromSecondTab] floatValue];
case ViewPagerOptionCenterCurrentTab:
return [[self centerCurrentTab] floatValue];
default:
return NAN;
}
}
-(void)setScrollEnabled:(BOOL)enabled forPageViewController:(UIPageViewController*)pageViewController{
for(UIView* view in pageViewController.view.subviews){
if([view isKindOfClass:[UIScrollView class]]){
UIScrollView* scrollView=(UIScrollView*)view;
[scrollView setScrollEnabled:enabled];
return;
}
}
}
- (void)defaultSettings {
// pageViewController
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
[self addChildViewController:self.pageViewController];
// Setup some forwarding events to hijack the scrollView
// Keep a reference to the actual delegate
self.actualDelegate = ((UIScrollView *)[self.pageViewController.view.subviews objectAtIndex:0]).delegate;
// Set self as new delegate
((UIScrollView *)[self.pageViewController.view.subviews objectAtIndex:0]).delegate = self;
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
self.animatingToTab = NO;
self.defaultSetupDone = NO;
}
- (void)defaultSetup {
// Empty tabs and contents
for (UIView *tabView in self.tabs) {
[tabView removeFromSuperview];
}
self.tabsView.contentSize = CGSizeZero;
[self.tabs removeAllObjects];
[self.contents removeAllObjects];
// Get tabCount from dataSource
self.tabCount = [self.dataSource numberOfTabsForViewPager:self];
// Populate arrays with [NSNull null];
self.tabs = [NSMutableArray arrayWithCapacity:self.tabCount];
for (NSUInteger i = 0; i < self.tabCount; i++) {
[self.tabs addObject:[NSNull null]];
}
self.contents = [NSMutableArray arrayWithCapacity:self.tabCount];
for (NSUInteger i = 0; i < self.tabCount; i++) {
[self.contents addObject:[NSNull null]];
}
// Add tabsView
self.tabsView = (UIScrollView *)[self.view viewWithTag:kTabViewTag];
if (!self.tabsView) {
self.tabsView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.frame), [self.tabHeight floatValue])];
self.tabsView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.tabsView.backgroundColor = self.tabsViewBackgroundColor;
self.tabsView.scrollsToTop = NO;
self.tabsView.showsHorizontalScrollIndicator = NO;
self.tabsView.showsVerticalScrollIndicator = NO;
self.tabsView.tag = kTabViewTag;
[self.view insertSubview:self.tabsView atIndex:0];
}
// Add tab views to _tabsView
CGFloat contentSizeWidth = 0;
// Give the standard offset if fixFormerTabsPositions is provided as YES
if ([self.fixFormerTabsPositions boolValue]) {
// And if the centerCurrentTab is provided as YES fine tune the offset according to it
if ([self.centerCurrentTab boolValue]) {
contentSizeWidth = (CGRectGetWidth(self.tabsView.frame) - [self.tabWidth floatValue]) / 2.0;
} else {
contentSizeWidth = [self.tabOffset floatValue];
}
}
for (NSUInteger i = 0; i < self.tabCount; i++) {
UIView *tabView = [self tabViewAtIndex:i];
CGRect frame = tabView.frame;
frame.origin.x = contentSizeWidth;
frame.size.width = [self.tabWidth floatValue];
tabView.frame = frame;
[self.tabsView addSubview:tabView];
contentSizeWidth += CGRectGetWidth(tabView.frame);
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[tabView addGestureRecognizer:tapGestureRecognizer];
}
contentSizeWidth=contentSizeWidth-500;
if ([self.fixLatterTabsPositions boolValue]) {
if ([self.centerCurrentTab boolValue]) {
contentSizeWidth += (CGRectGetWidth(self.tabsView.frame) - [self.tabWidth floatValue]) / 2.0;
} else {
contentSizeWidth += CGRectGetWidth(self.tabsView.frame) - [self.tabWidth floatValue] - [self.tabOffset floatValue];
}
}
self.tabsView.contentSize = CGSizeMake(contentSizeWidth, [self.tabHeight floatValue]);
self.contentView = [self.view viewWithTag:kContentViewTag];
if (!self.contentView) {
self.contentView = self.pageViewController.view;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// self.contentView.backgroundColor = self.contentViewBackgroundColor;
self.contentView.bounds = self.view.bounds;
self.contentView.tag = kContentViewTag;
[self.view insertSubview:self.contentView atIndex:0];
}
NSUInteger index = [self.startFromSecondTab boolValue] ? 1 : 0;
[self selectTabAtIndex:index];
self.defaultSetupDone = YES;
}
- (TabView *)tabViewAtIndex:(NSUInteger)index {
if (index >= self.tabCount) {
return nil;
}
if ([[self.tabs objectAtIndex:index] isEqual:[NSNull null]]) {
UIView *tabViewContent = [self.dataSource viewPager:self viewForTabAtIndex:index];
tabViewContent.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
TabView *tabView = [[TabView alloc] initWithFrame:CGRectMake(0.0, 0.0, [self.tabWidth floatValue], [self.tabHeight floatValue])];
[tabView addSubview:tabViewContent];
[tabView setClipsToBounds:YES];
[tabView setIndicatorColor:self.indicatorColor];
tabViewContent.center = tabView.center;
// Replace the null object with tabView
[self.tabs replaceObjectAtIndex:index withObject:tabView];
}
return [self.tabs objectAtIndex:index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
if (index >= self.tabCount) {
return nil;
}
if ([[self.contents objectAtIndex:index] isEqual:[NSNull null]]) {
UIViewController *viewController;
if ([self.dataSource respondsToSelector:@selector(viewPager:contentViewControllerForTabAtIndex:)]) {
viewController = [self.dataSource viewPager:self contentViewControllerForTabAtIndex:index];
} else if ([self.dataSource respondsToSelector:@selector(viewPager:contentViewForTabAtIndex:)]) {
UIView *view = [self.dataSource viewPager:self contentViewForTabAtIndex:index];
// Adjust view's bounds to match the pageView's bounds
UIView *pageView = [self.view viewWithTag:kContentViewTag];
view.frame = pageView.bounds;
viewController = [UIViewController new];
viewController.view = view;
} else {
viewController = [[UIViewController alloc] init];
viewController.view = [[UIView alloc] init];
}
[self.contents replaceObjectAtIndex:index withObject:viewController];
}
return [self.contents objectAtIndex:index];
}
- (NSUInteger)indexForViewController:(UIViewController *)viewController {
return [self.contents indexOfObject:viewController];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [self indexForViewController:viewController];
index++;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [self indexForViewController:viewController];
index--;
return [self viewControllerAtIndex:index];
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
UIViewController *viewController = self.pageViewController.viewControllers[0];
// Select tab
NSUInteger index = [self indexForViewController:viewController];
[self selectTabAtIndex:index];
}
我不想让我们在这个pageviewController上滑动我只想处理带有下一个和后退按钮的分页,但是我无法禁用滑动手势。是否可以禁用它,或者这是否可以通过其他方式删除该视图中的所有手势?
答案 0 :(得分:14)
试试这个
for (UIScrollView *view in self.pageViewController.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
view.scrollEnabled = NO;
}
}