在程度 的入口,键入
[ChooseControllerTool chooseRootController];
ChooseControllerTool 工具类。
ChooseControllerTool.h
@interface ChooseControllerTool : NSObject
/**
* 选择根控制器
*/
+ (void)chooseRootController;
@end
ChooseControllerTool.m
#import "ChooseControllerTool.h"
#import "DHSlideMenuController.h"
#import "NewfeatureViewController.h"
@implementation ChooseControllerTool
+ (void)chooseRootController
{
NSString *key = @"CFBundleVersion";
// 取出沙盒中存储的上次使用软件的版本号
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastVersion = [defaults stringForKey:key];
// 获得当前软件的版本号
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
if ([currentVersion isEqualToString:lastVersion]) {
// 显示状态栏
[UIApplication sharedApplication].statusBarHidden = NO;
[UIApplication sharedApplication].keyWindow.rootViewController = [DHSlideMenuController sharedInstance];
[[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"NewbieGuide"];
} else { // 新版本
[UIApplication sharedApplication].keyWindow.rootViewController = [[NewfeatureViewController alloc] init];
// 存储新版本
[defaults setObject:currentVersion forKey:key];
[defaults synchronize];
[[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"NewbieGuide"];//新手引导的标识
}
}
@end
NewfeatureViewController.m
#import "WarmHeart.pch"
#import "DHSlideMenuController.h"
#import "UIColor+Utils.h"
#define NewfeatureImageCount 3
#define ButtonColor @"282828"
#define pageControlColor @"282828"
#define pageBackColor @"ccc"
@interface NewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@end
@implementation NewfeatureViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
// 1.添加UIScrollView
[self setupScrollView];
// 2.添加pagePageController
[self setupPageControl];
}
/**
* 添加pageControl
*/
- (void)setupPageControl
{
// 1.添加
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.numberOfPages = NewfeatureImageCount;
CGFloat centerX = self.view.frame.size.width * 0.5;
CGFloat centerY = self.view.frame.size.height - 30;
pageControl.center = CGPointMake(centerX, centerY);
pageControl.bounds = CGRectMake(0, 0, 100, 30);
pageControl.userInteractionEnabled = NO;
[self.view addSubview:pageControl];
self.pageControl = pageControl;
// 2.设置圆点的颜色
pageControl.currentPageIndicatorTintColor =[UIColor colorFromString:pageControlColor];
pageControl.pageIndicatorTintColor = [UIColor colorFromString:pageBackColor];
}
- (void)setupScrollView
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.delegate = self;
[self.view addSubview:scrollView];
// 2.添加图片
CGFloat imageW = scrollView.frame.size.width;
CGFloat imageH = scrollView.frame.size.height;
for (int index = 0; index < NewfeatureImageCount; index++) {
UIImageView *imageView = [[UIImageView alloc] init];
// 设置图片
NSString *name = nil;
if (kScreenHeight == 568) { // 4inch
name = [NSString stringWithFormat:@"new_feature_%d-568h.jpg", index + 1];
} else if (kScreenHeight == 667) { // 4.7inch
name = [NSString stringWithFormat:@"new_feature_%d-667h.jpg", index + 1];
} else if (kScreenHeight == 736) { // 5.5inch
name = [NSString stringWithFormat:@"new_feature_%d-736h.jpg", index + 1];
} else { // 3.5inch
name = [NSString stringWithFormat:@"new_feature_%d-480h.jpg", index + 1];
}
imageView.image = [UIImage imageNamed:name];
// 设置frame
CGFloat imageX = index * imageW;
imageView.frame = CGRectMake(imageX, 0, imageW, imageH);
[scrollView addSubview:imageView];
// 在最后一个图片上面添加按钮
if (index == NewfeatureImageCount - 1) {
[self setupLastImageView:imageView];
}
}
// 3.设置滚动的内容尺寸
scrollView.contentSize = CGSizeMake(imageW * NewfeatureImageCount, 0);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
}
/**
* 添加内容到最后一个图片
*/
- (void)setupLastImageView:(UIImageView *)imageView
{
// 0.让imageView能跟用户交互
imageView.userInteractionEnabled = YES;
// 1.添加开始按钮
UIButton *startButton = [[UIButton alloc] init];
[startButton setTitle:@"开启 暖·心理" forState:UIControlStateNormal];
startButton .titleLabel.font = [UIFont systemFontOfSize:16];
[startButton setTitleColor:[UIColor colorFromString:ButtonColor] forState:UIControlStateNormal];
startButton.layer.borderWidth = 0.7;
startButton.layer.borderColor = [UIColor colorFromString:ButtonColor].CGColor;
if (kScreenHeight == 480) { // 3.5inch
startButton.frame = CGRectMake((68 * kScreenWidth / 320), kScreenHeight - (70 * kScreenHeight / 568) - (45 * kScreenHeight / 568) + 15, kScreenWidth - 2 * (68 * kScreenWidth / 320), (45 * kScreenHeight / 568));
} else if (kScreenHeight == 568) { // 4inch
startButton.frame = CGRectMake((68 * kScreenWidth / 320), kScreenHeight - (65 * kScreenHeight / 568) - (45 * kScreenHeight / 568), kScreenWidth - 2 * (68 * kScreenWidth / 320), (40 * kScreenHeight / 568));
} else if (kScreenHeight == 667) { // 4.7inch
startButton.frame = CGRectMake((68 * kScreenWidth / 320), kScreenHeight - (75 * kScreenHeight / 568) - (45 * kScreenHeight / 568), kScreenWidth - 2 * (68 * kScreenWidth / 320), (40 * kScreenHeight / 568));
} else { // 5.5inch
startButton.frame = CGRectMake((68 * kScreenWidth / 320), kScreenHeight - (75 * kScreenHeight / 568) - (45 * kScreenHeight / 568), kScreenWidth - 2 * (68 * kScreenWidth / 320), (40 * kScreenHeight / 568));
}
startButton.center = CGPointMake(kScreenWidth * 0.5, startButton.center.y);
startButton .layer.masksToBounds = YES;
startButton .layer.cornerRadius = (40 * kScreenHeight / 568)/2;
// 3.设置文字
[startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
// startButton.backgroundColor = [UIColor redColor];
[imageView addSubview:startButton];
}
/**
* 开始体验
*/
- (void)start
{
// 显示状态栏
[UIApplication sharedApplication].statusBarHidden = NO;
// 切换窗口的根控制器
self.view.window.rootViewController = [DHSlideMenuController sharedInstance];
}
/**
* 只要UIScrollView滚动了,就会调用
*
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 1.取出水平方向上滚动的距离
CGFloat offsetX = scrollView.contentOffset.x;
// 2.求出页码
double pageDouble = offsetX / scrollView.frame.size.width;
int pageInt = (int)(pageDouble + 0.5);
self.pageControl.currentPage = pageInt;
}
@end