码迷,mamicode.com
首页 > 其他好文 > 详细

UIPageViewController使用示例

时间:2015-09-13 21:23:01      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

#import "MediumController.h"
#import "PhotoViewController.h"
#import "VideoViewController.h"

@interface MediumController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
{
    NSMutableArray *_pageArray;
    UIPageViewController *_pageCtrl;
    UISegmentedControl *_segCtrl;
    
    //当前页
    NSInteger _curPage;
}
@end

@implementation MediumController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTopView];
    [self createPageData];
    [self createPagrVC];
    
    [self createBackBtn];
    
    self.view.backgroundColor = [UIColor whiteColor];
}
//创建顶部按钮
-(void)createTopView
{
    _segCtrl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"预告片",@"剧照", nil]];
    [_segCtrl addTarget:self action:@selector(clickSegCtrl:) forControlEvents:UIControlEventValueChanged];
    [_segCtrl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    [_segCtrl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
    [_segCtrl setTintColor:[UIColor whiteColor]];
    
    _segCtrl.frame = CGRectMake(100, 7, kScreenWidth-200, 30);
    if (_type == MoviesType) {
        _segCtrl.selectedSegmentIndex = 0;
    }
    else if(_type == PhotoType)
    {
        _segCtrl.selectedSegmentIndex = 1;
    }
    self.navigationItem.titleView = _segCtrl;
}
//创建pagecontrol
-(void)createPagrVC
{
    NSInteger index = _type;
    _pageCtrl = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    _pageCtrl.delegate = self;
    _pageCtrl.dataSource = self;
    [_pageCtrl setViewControllers:@[_pageArray[index]] direction:UIPageViewControllerNavigationDirectionForward animated:nil completion:nil];
    _pageCtrl.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:_pageCtrl.view];
}
//创建数据源
-(void)createPageData
{
    _pageArray = [NSMutableArray array];
    VideoViewController *vvc = [[VideoViewController alloc] init];
    vvc.videoArray = _videoArray;
    [_pageArray addObject:vvc];
    PhotoViewController *pvc = [[PhotoViewController alloc] init];
    pvc.photoArray = _photoArray;
    [_pageArray addObject:pvc];
    
}

-(void)clickSegCtrl:(UISegmentedControl *)ctrl
{
    NSInteger index = ctrl.selectedSegmentIndex;
    [_pageCtrl setViewControllers:@[_pageArray[index]] direction:index<_curPage animated:YES completion:^(BOOL finished){
        _curPage = index;
    }];
}

#pragma mark --UIPageViewController代理----
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [_pageArray indexOfObject:viewController];
    if (index+1 == _pageArray.count) {
        return nil;
    }
    return _pageArray[index+1];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [_pageArray indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }
    return _pageArray[index-1];
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    _curPage = [_pageArray indexOfObject:pageViewController.viewControllers[0]];
    _segCtrl.selectedSegmentIndex = _curPage;
}

@end

 

UIPageViewController使用示例

标签:

原文地址:http://www.cnblogs.com/liaods/p/4805448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!