关闭

ios UIPageViewController+pageControl实现引导页

2195人阅读 评论(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网站的观点或立场