关闭

ios UIPageViewController+pageControl实现引导页

2539人阅读 评论(0) 收藏 举报
分类:

1,首先准备一个控制器,在它里面添加一个UIPageViewController,然后添加要引导的控制器

头文件:

#import <UIKit/UIKit.h>
@interface PVCPagesViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
@property (strong, nonatomic)  UIPageControl *pageControl;
@end

.m文件:

#import "PVCPagesViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"

#define MYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
@interface PVCPagesViewController () {
    NSArray *pages;
}

@property (strong, nonatomic) NSArray *pages;
@property (strong, nonatomic) UIPageViewController *pageController;

@end
@implementation PVCPagesViewController

@synthesize pageControl;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self buildUI];
    self.pages =@[
                  [[ViewController1 alloc]init],
                  [[ViewController2 alloc]init],
                  [[ViewController3 alloc]init],
                  [[ViewController4 alloc]init]
                ];

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    [self.pageController setDelegate:self];
    [self.pageController setDataSource:self];

    [[self.pageController view] setFrame:[[self view] bounds]];
    NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:0]];
    [self.pageControl setCurrentPage:0];
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

    
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    [self addChildViewController:self.pageController];
    
    [self.view addSubview:self.pageControl];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];
    
    [self.view sendSubviewToBack:[self.pageController view]];
}

//也可不在viewControllerAfterViewController和viewControllerBeforeViewController中设置pageConroll,采取以下方法设置
//- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
//{
//    NSInteger index= [self.pages indexOfObject:[pendingViewControllers objectAtIndex:0]];
//    [self.pageControl setCurrentPage:index];
//}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
    NSUInteger currentIndex = [self.pages indexOfObject:viewController];
    [self.pageControl setCurrentPage:self.pageControl.currentPage+1];
    if ( currentIndex < [self.pages count]-1) {
        return [self.pages objectAtIndex:currentIndex+1];
    } else {
        return nil;
    }
}


- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    NSUInteger currentIndex = [self.pages indexOfObject:viewController];
    [self.pageControl setCurrentPage:self.pageControl.currentPage-1];
    if ( currentIndex > 0) {
        return [self.pages objectAtIndex:currentIndex-1];
    } else {
        return nil;
    }
}
-(void)buildUI
{
    // init pageControl
    pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = 4;
    pageControl.currentPage = 0;
    [pageControl setBounds:CGRectMake(0,0,16*(4-1)+16,16)]; //页面控件上的圆点间距基本在16左右。
    [pageControl.layer setCornerRadius:8]; // 圆角层
    [pageControl setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.2]];
    [self.view addSubview:pageControl];
    
    pageControl.frame=CGRectMake(50, 200, 40, 80);
    
    pageControl.currentPageIndicatorTintColor =MYColor(13, 109, 255);
    pageControl.pageIndicatorTintColor = MYColor(47, 47, 47);
    pageControl.backgroundColor = [UIColor clearColor];
    [self.pageControl setCurrentPage:0];
    
}

- (void)changePage:(id)sender {
    
    UIViewController *visibleViewController = self.pageController.viewControllers[0];
    NSUInteger currentIndex = [self.pages indexOfObject:visibleViewController];
    
    NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:self.pageControl.currentPage]];
    
    if (self.pageControl.currentPage > currentIndex) {
        [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    } else {
        [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
        
    }
}

@end

ViewController1的.h文件:

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

ViewController1的.m文件:

#import "ViewController1.h"

@interface ViewController1 ()

@end

@implementation ViewController1

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLabel];
    self.view.backgroundColor=[UIColor purpleColor];
}
-(void)addLabel
{
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
    [label setText:@" 1VC"];
    [self.view addSubview:label];
    
}
@end

其他三个控制器与VC1一样,只不过背景不一样罢了

0
1
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

IOS控件的应用UIPageController与UIScrollView

UIPageController是大多数IOS教程上不会写的系统自带控件。它主要的功能是实现视图分页,有点类似于网页上的分页功能。 这次主要通过一个例子来讲讲UIPageController控件...
  • xcysuccess3
  • xcysuccess3
  • 2012-09-06 14:53
  • 7874

UIPageViewController的使用(Swift)

class ViewController: UIViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {...
  • lcl130
  • lcl130
  • 2014-12-24 09:29
  • 3645

swift+UIPageViewController 纯代码实现引导页

使用UIPageViewController1、介绍:UIPageViewController是一个类似UINavigationController的Controller容器。它既可以实现UIScro...
  • u012297622
  • u012297622
  • 2016-08-29 18:35
  • 2012

iOS UIPageViewController - 使用总结

最近在一个项目中需要一种以翻书的形式来展示图片的功能,在查阅了一些资料后,初步了解了UIPageViewController,查到的资料都是比较久之前的,有的还是在MRC的,在使用的过程中也遇到一些问...
  • lqq200912408
  • lqq200912408
  • 2015-12-27 16:35
  • 4207

IOS——使用ScrollView和PageController实现引导页分页效果

如标题所说,使用的是ScrollView和PageControlle两个控件共同达到该效果 首先,使用storyboard将ScrollView和PageControlle拖入,要注意的是,Page...
  • u012476249
  • u012476249
  • 2016-01-11 15:31
  • 4607

《UIPageControl 的创建和 自定义和引导页的使用》

本篇文章主要讲解 Uipagecontrol 的创建和其属性的介绍。还有 UIpageControl 的自定义样式。还有现代开发不可缺少的引导页的创建和详细使用。 现在的App开发,都离不开一个...
  • zhoushuangjian511
  • zhoushuangjian511
  • 2015-12-15 09:09
  • 1218

IOS系列——UIPageControl的基本属性

self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(120,440,100,18)]; // 初始化mypagecont...
  • Engandend
  • Engandend
  • 2013-11-06 14:23
  • 1211

pagecontroller最简单的方法

头文件.h#import #import "ViewController1.h"#import "ViewController2.h"@interface pagecontrol : UIViewCo...
  • windcxb
  • windcxb
  • 2010-09-08 09:30
  • 3613

ios菜鸟之路:uiscrollview和pagecontrol的使用方法

一、UIScrollview以及pagecontrol的声明 @interface rootViewController : UIViewController//加上scrollviewdelega...
  • panyong4627
  • panyong4627
  • 2012-07-19 14:32
  • 5047

IOS之PageControl和ScrollView搭配

// //  ScrollViewController.m //  Scroll // //  Created by book mac on 13-1-20. //  Copyright (...
  • xiaoclove
  • xiaoclove
  • 2013-01-20 19:35
  • 2349