#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
时间: 2024-10-12 22:32:34