UIPageViewController 实现从中间翻页效果

最近两天一直在研究如何实现从中间翻页,问了很多人,也在网上查了很多资料,现在终于弄明白了。。。原来如此简单。。。

创建项目

启动Xcode,新建iOS  Empty Application 项目

创建PageAppViewController

新建PageAppViewController项目,继承自UIPageViewController,如图:

PageViewController.h中代码如下:

#import <UIKit/UIKit.h>


@interface PageViewController :UIPageViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property(retain,nonatomic)NSArray * pageContent;

@property(retain,nonatomic)NSMutableArray * arr;


@end

PageViewController.m中代码:

#import "PageViewController.h"


@interface PageViewController ()


@end


@implementation PageViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    self.arr = [NSMutableArrayarrayWithCapacity:0];

    [selfcreateContentPages];

    

    

    self.dataSource =self;

   self.delegate =self;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (void)createContentPages{

   for (int i =0; i < 10; i ++) {

        UIViewController * viewCtl = [[UIViewControlleralloc]init];

        viewCtl.view.backgroundColor = [UIColorredColor];

        [self.arraddObject:viewCtl];

    }

    [selfsetViewControllers:@[self.arr[0]]direction:(UIPageViewControllerNavigationDirectionForward)animated:NOcompletion:nil];

    self.pageContent = [[NSArrayalloc]initWithArray:self.arr];

}

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{

   NSArray * viewControllers = @[self.arr[0],self.arr[1]];

    [selfsetViewControllers:viewControllers direction:(UIPageViewControllerNavigationDirectionForward)animated:NOcompletion:nil];

    returnUIPageViewControllerSpineLocationMid;

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

   int index = [self.arrindexOfObject:viewController];

   if (index >= 0 && index <self.arr.count -1) {

       return [self.arrobjectAtIndex:index + 1];

    }

    return nil;

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{

   int index = [self.arrindexOfObject:viewController];

   if (index > 0 && index <self.arr.count -1) {

       return [self.arrobjectAtIndex:index - 1];

    }

    return nil;

}

@end


然后运行就可以了。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值