如何使用页面控件在视图控制器之间滚动

时间:2016-12-28 09:09:38

标签: ios objective-c uiviewcontroller uiscrollview uipagecontrol

screenshots

我想滚动或移动到其他视图。如何使用页面控制?

3 个答案:

答案 0 :(得分:0)

系统提供 UIPageViewController ,您可以使用它和UIPageControl来获取您的想法

答案 1 :(得分:0)

您可以使用pageControl的属性currentPage

@property(nonatomic) NSInteger currentPage;            // default is 0. value pinned to 0..numberOfPages-1

可以用作

[pageControl setCurrentPage:<logic to get your VC>];

答案 2 :(得分:0)

pages是一个数组,您将存储storyboard id和NSInteger currentPageIndex,设置currentPageIndex = 0

- (void)viewDidLoad
{
self.pages = [[NSMutableArray alloc]init];

// instantiate the view controlles from the storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController* ViewController1 = [storyboard instantiateViewControllerWithIdentifier:@"VC1"];

UIViewController* ViewController2 = [storyboard instantiateViewControllerWithIdentifier:@"VC2"];

// load the view controllers in our pages array
[self.pages addObjectsFromArray:@[ViewController1, ViewController2]];
// your general suff
}


 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSInteger index = [_pages indexOfObject:viewController];

if ((index == NSNotFound) || (index == 0)) {
    return nil;
}

index--;
return [_pages objectAtIndex:index];
}


- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSInteger index = [_pages indexOfObject:viewController];

if (index == NSNotFound) {
    return nil;
}
index++;

if (index == [_pages count]) {
    return nil;
}
return [_pages objectAtIndex:index];
}


-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
if (completed) {
    self.currentPageIndex = [_pages indexOfObject:[pageViewController.viewControllers lastObject]];
}
}