iOS 关于引导页的工具类的封装 一句代码搞定引导页

本文介绍了一个iOS应用在不同版本启动时的新特性展示逻辑。通过检查应用版本并相应地调整显示内容,实现了新版本特性介绍页面的功能,并在用户完成引导后切换到主界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在程度 的入口,键入 

   [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

 

转载于:https://my.oschina.net/yanxue1825/blog/708740

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值