UIPageViewController实现的左右滑动界面

 更新时间:2018年06月30日 13:04:59   作者:Ittttttttta  
这篇文章主要为大家详细介绍了UIPageViewController实现的左右滑动界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 仿微博客户端红包加载界面 XLDotLoading效果

    iOS 仿微博客户端红包加载界面 XLDotLoading效果

    这篇文章主要介绍了iOS 仿微博客户端红包加载界面 XLDotLoading,需要的朋友可以参考下
    2017-02-02
  • iOS开发实现搜索框(UISearchController)

    iOS开发实现搜索框(UISearchController)

    这篇文章主要为大家详细介绍了iOS开发实现搜索框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • iOS实现雷达扫描效果

    iOS实现雷达扫描效果

    这篇文章主要为大家详细介绍了iOS实现雷达扫描效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • iOS UISegmentControl实现自定义分栏效果

    iOS UISegmentControl实现自定义分栏效果

    这篇文章主要为大家详细介绍了iOS UISegmentControl实现自定义分栏效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • iOS中sqlite的详细用法

    iOS中sqlite的详细用法

    在iOS中,也同样支持sqlite。目前有很多第三方库,封装了sqlite操作,比如swift语言写的SQLite.swift,对sqlite感兴趣的小伙伴们可以参考一下
    2016-05-05
  • iOS 对象属性详细介绍

    iOS 对象属性详细介绍

    这篇文章主要介绍了iOS 对象属性详细介绍的相关资料,这里整理了IOS 对象的相关资料,需要的朋友可以参考下
    2016-11-11
  • iOS常见算法以及应用知识点总结

    iOS常见算法以及应用知识点总结

    在本篇文章里小编给大家分享的是关于iOS常见算法以及应用知识点总结,有兴趣的朋友们学习下。
    2019-10-10
  • IOS 实现一个死锁导致 UI 假死的例子

    IOS 实现一个死锁导致 UI 假死的例子

    这篇文章主要介绍了IOS 实现一个死锁导致 UI 假死的例子的相关资料,需要的朋友可以参考下
    2016-12-12
  • 详谈iPhoneX截图如何带

    详谈iPhoneX截图如何带

    下面小编就为大家分享一篇详谈iPhoneX截图如何带"刘海"和圆角,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • iOS开发自定义页脚和页眉技巧详解

    iOS开发自定义页脚和页眉技巧详解

    这篇文章主要为大家介绍了iOS开发自定义页脚和页眉的技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07

最新评论