上述代码中的图片名称是写死在GuidePageViewController中的,根视图控制器也是写死的,如果其他App想要复用该功能,就要进入该代码修改这两哥地方,为了不修改一行代码就可以使用该功能,可以将这两个参数提取出来,初始化该控制器时作为参数传递
封装示例代码
初始化时以参数的形式将图片和根视图控制器传递给引导页视图控制器
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = [[GuidePageViewController alloc] initWithImages:@[@"guide-page-1", @"guide-page-2", @"guide-page-3", @"guide-page-4"] rootViewController:viewController];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface GuidePageViewController : UIViewController
- (instancetype)initWithImages:(NSArray *)images rootViewController:(UIViewController *)rootViewController;
@end
在初始化方法中将引导页图片和根视图控制器保存起来,使用self.images.count获取引导页数量,引导页图片直接从images数组中获取
#import "GuidePageViewController.h"
#import "ViewController.h"
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)
@interface GuidePageViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) UIPageControl *pageControl;
@property (weak, nonatomic) UIButton *startAppButton;
@property (strong, nonatomic) NSArray *images;
@property (strong, nonatomic) UIViewController *rootViewController;
@end
@implementation GuidePageViewController
- (instancetype)initWithImages:(NSArray *)images rootViewController:(UIViewController *)rootViewController {
if (self = [super init]) {
_images = images;
_rootViewController = rootViewController;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// UIScrollView
UIScrollView *guidePageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
guidePageScrollView.contentSize = CGSizeMake(kScreenWidth * self.images.count, 0);
guidePageScrollView.showsHorizontalScrollIndicator = NO;
guidePageScrollView.pagingEnabled = YES;
guidePageScrollView.bounces = NO;
guidePageScrollView.delegate = self;
for (int i = 0; i < self.images.count; i++) {
UIImageView *guideImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight)];
guideImageView.image = [UIImage imageNamed:self.images[i]];
[guidePageScrollView addSubview:guideImageView];
}
[self.view addSubview:guidePageScrollView];
// UIPageControl
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - 100) / 2, kScreenHeight- 50, 100, 30)];
pageControl.numberOfPages = self.images.count;
pageControl.currentPage = 0;
pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
[self.view addSubview:pageControl];
self.pageControl = pageControl;
UIButton *startAppButton = [UIButton buttonWithType:UIButtonTypeCustom];
startAppButton.frame = CGRectMake((kScreenWidth - 100) / 2, 550, 100, 40);
[startAppButton setTitle:@"立即体验" forState:UIControlStateNormal];
startAppButton.backgroundColor = [UIColor grayColor];
[startAppButton addTarget:self action:@selector(startAppAction) forControlEvents:UIControlEventTouchUpInside];
startAppButton.hidden = YES;
[self.view addSubview:startAppButton];
_startAppButton = startAppButton;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSInteger currentPage = scrollView.contentOffset.x / kScreenWidth;
self.pageControl.currentPage = currentPage;
if (currentPage == (self.images.count - 1)) {
self.startAppButton.hidden = NO;
}
}
- (void)startAppAction {
[UIApplication sharedApplication].keyWindow.rootViewController = self.rootViewController;
}
@end










