当前位置:   article > 正文

ios UIPageViewController+pageControl实现引导页_pagecontroller 图片

pagecontroller 图片

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

头文件:

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

.m文件:

  1. #import "PVCPagesViewController.h"
  2. #import "ViewController1.h"
  3. #import "ViewController2.h"
  4. #import "ViewController3.h"
  5. #import "ViewController4.h"
  6. #define MYColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
  7. @interface PVCPagesViewController () {
  8. NSArray *pages;
  9. }
  10. @property (strong, nonatomic) NSArray *pages;
  11. @property (strong, nonatomic) UIPageViewController *pageController;
  12. @end
  13. @implementation PVCPagesViewController
  14. @synthesize pageControl;
  15. - (void)viewDidLoad
  16. {
  17. [super viewDidLoad];
  18. [self buildUI];
  19. self.pages =@[
  20. [[ViewController1 alloc]init],
  21. [[ViewController2 alloc]init],
  22. [[ViewController3 alloc]init],
  23. [[ViewController4 alloc]init]
  24. ];
  25. self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
  26. [self.pageController setDelegate:self];
  27. [self.pageController setDataSource:self];
  28. [[self.pageController view] setFrame:[[self view] bounds]];
  29. NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:0]];
  30. [self.pageControl setCurrentPage:0];
  31. [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
  32. [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  33. [self addChildViewController:self.pageController];
  34. [self.view addSubview:self.pageControl];
  35. [[self view] addSubview:[self.pageController view]];
  36. [self.pageController didMoveToParentViewController:self];
  37. [self.view sendSubviewToBack:[self.pageController view]];
  38. }
  39. //也可不在viewControllerAfterViewController和viewControllerBeforeViewController中设置pageConroll,采取以下方法设置
  40. //- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
  41. //{
  42. // NSInteger index= [self.pages indexOfObject:[pendingViewControllers objectAtIndex:0]];
  43. // [self.pageControl setCurrentPage:index];
  44. //}
  45. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
  46. NSUInteger currentIndex = [self.pages indexOfObject:viewController];
  47. [self.pageControl setCurrentPage:self.pageControl.currentPage+1];
  48. if ( currentIndex < [self.pages count]-1) {
  49. return [self.pages objectAtIndex:currentIndex+1];
  50. } else {
  51. return nil;
  52. }
  53. }
  54. - (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
  55. NSUInteger currentIndex = [self.pages indexOfObject:viewController];
  56. [self.pageControl setCurrentPage:self.pageControl.currentPage-1];
  57. if ( currentIndex > 0) {
  58. return [self.pages objectAtIndex:currentIndex-1];
  59. } else {
  60. return nil;
  61. }
  62. }
  63. -(void)buildUI
  64. {
  65. // init pageControl
  66. pageControl = [[UIPageControl alloc] init];
  67. pageControl.numberOfPages = 4;
  68. pageControl.currentPage = 0;
  69. [pageControl setBounds:CGRectMake(0,0,16*(4-1)+16,16)]; //页面控件上的圆点间距基本在16左右。
  70. [pageControl.layer setCornerRadius:8]; // 圆角层
  71. [pageControl setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.2]];
  72. [self.view addSubview:pageControl];
  73. pageControl.frame=CGRectMake(50, 200, 40, 80);
  74. pageControl.currentPageIndicatorTintColor =MYColor(13, 109, 255);
  75. pageControl.pageIndicatorTintColor = MYColor(47, 47, 47);
  76. pageControl.backgroundColor = [UIColor clearColor];
  77. [self.pageControl setCurrentPage:0];
  78. }
  79. - (void)changePage:(id)sender {
  80. UIViewController *visibleViewController = self.pageController.viewControllers[0];
  81. NSUInteger currentIndex = [self.pages indexOfObject:visibleViewController];
  82. NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:self.pageControl.currentPage]];
  83. if (self.pageControl.currentPage > currentIndex) {
  84. [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  85. } else {
  86. [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
  87. }
  88. }
  89. @end

ViewController1的.h文件:

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

ViewController1的.m文件:

  1. #import "ViewController1.h"
  2. @interface ViewController1 ()
  3. @end
  4. @implementation ViewController1
  5. - (void)viewDidLoad {
  6. [super viewDidLoad];
  7. [self addLabel];
  8. self.view.backgroundColor=[UIColor purpleColor];
  9. }
  10. -(void)addLabel
  11. {
  12. UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];
  13. [label setText:@" 1VC"];
  14. [self.view addSubview:label];
  15. }
  16. @end

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/709281
推荐阅读
相关标签
  

闽ICP备14008679号