欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

UIPageViewController实现的左右滑动界面

时间:2021-01-13 09:58:45|栏目:iOS代码|点击:

本文实例为大家分享了UIPageViewController实现左右滑动界面展示的具体代码,供大家参考,具体内容如下


.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

@end

.m

#import "ViewController.h"
#import "SubPage1ViewController.h"
#import "SubPage2ViewController.h"
@interface ViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
@property NSArray *contentViewControllers; //ViewControllers
@property (nonatomic, strong) UIPageViewController *pageViewController;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self pageViewController];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
#pragma mark
#pragma mark ----- UIPageViewControllerDataSource -----
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
  NSUInteger index = [self indexForViewController:viewController];
  if (index == 0) {
    index = [self.contentViewControllers count] - 1;
  } else {
    index--;
  }
  return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
  NSUInteger index = [self indexForViewController:viewController];
  index++;
  if (index == [self.contentViewControllers count]) {
    index = 0;
  }
  return [self viewControllerAtIndex:index];
}
- (NSUInteger)indexForViewController:(UIViewController *)viewController
{
  return [self.contentViewControllers indexOfObject:viewController];
}

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
  if (index > [self.contentViewControllers count]) {
    return nil;
  }
  UIViewController *vc = [self.contentViewControllers objectAtIndex:index];
  return vc;
}
#pragma mark
#pragma mark ----- UIPageViewControllerDelegate -----
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{


}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
}
#pragma mark
#pragma mark Init
- (UIPageViewController *)pageViewController
{
  if(!_pageViewController)
  {
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
                              forKey: UIPageViewControllerOptionSpineLocationKey];
    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                               navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                      options:options];
    [[_pageViewController view] setFrame:[[self view] bounds]];
    _contentViewControllers = [NSMutableArray arrayWithObjects:[SubPage1ViewController new],[SubPage2ViewController new],nil];
    // 设置UIPageViewController的配置项
    [_pageViewController setViewControllers:[NSArray arrayWithObjects:_contentViewControllers[0], nil]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO
                   completion:nil];
    _pageViewController.delegate = self;
    _pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];

  }
  return _pageViewController;

}

上一篇:iOS应用开发中使用UIScrollView控件来实现图片缩放

栏    目:iOS代码

下一篇:IOS文件的简单读写实例详解

本文标题:UIPageViewController实现的左右滑动界面

本文地址:http://www.codeinn.net/misctech/44715.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有