欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS 实现引导页

程序员文章站 2022-07-05 23:05:35
效果图明天更新, 今天csdn维护. 直接上代码(简简单单) 创建一个控制器 .h @protocol selectDelegate - (void)click;...

效果图明天更新, 今天csdn维护.
直接上代码(简简单单)
创建一个控制器

.h
@protocol selectDelegate 
- (void)click;
@end
@interface XTGuidePagesViewController : UIViewController
@property (nonatomic, strong) UIButton *btnEnter;
// 初始化引导页
- (void)initWithXTGuideView:(NSArray *)images;
// 版本信息判断
- (BOOL)isShow;
@property (nonatomic, assign) id delegate;
// 创建单利类
+ (instancetype)shareXTGuideVC;
.m
- (void)initWithXTGuideView:(NSArray *)images
{
    UIScrollView *gui = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, s_w, s_h)];
    gui.pagingEnabled = YES;
    // 隐藏滑动条
    gui.showsHorizontalScrollIndicator = NO;
    gui.showsVerticalScrollIndicator = NO;
    // 取消反弹
    gui.bounces = NO;
    for (NSInteger i = 0; i < images.count; i ++) {
        [gui addSubview:({
            self.btnEnter = [UIButton buttonWithType:UIButtonTypeCustom];
            self.btnEnter.frame = CGRectMake(s_w * i, 0, s_w, s_h);
            [self.btnEnter setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];;
            self.btnEnter;
        })];

        [self.btnEnter addSubview:({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:@"点击进入" forState:UIControlStateNormal];
            btn.frame = CGRectMake(s_w * i, s_h - 60, 100, 40);
            btn.center = CGPointMake(s_w / 2, s_h - 60);
            btn.backgroundColor = [UIColor lightGrayColor];
            [btn addTarget:self action:@selector(clickEnter) forControlEvents:UIControlEventTouchUpInside];
            btn;
        })];
    }
    gui.contentSize = CGSizeMake(s_w * images.count, 0);
    [self.view addSubview:gui];
}

单利

+ (instancetype)shareXTGuideVC
{
    static XTGuidePagesViewController *x = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        x = [[XTGuidePagesViewController alloc] init];
    });
    return x;
}
- (void)clickEnter
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(click)]) {
        [self.delegate click];
    }
}
- (BOOL)isShow
{
    // 读取版本信息
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *localVersion = [user objectForKey:VERSION_INFO_CURRENT];
    NSString *currentVersion =[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"L ===%@", localVersion);
    NSLog(@"C ===%@", currentVersion);
    if (localVersion == nil || ![currentVersion isEqualToString:localVersion]) {
        [self saveCurrentVersion];
        return YES;
    }else
    {
        return NO;
    }
}
// 保存版本信息
- (void)saveCurrentVersion
{
    NSString *version =[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:version forKey:VERSION_INFO_CURRENT];
    [user synchronize];
}

在app delegate中调用

NSArray *images = @[@"1", @"2", @"3"];
    if ([[[XTGuidePagesViewController alloc] init] isShow]) {
        self.window.rootViewController = [XTGuidePagesViewController shareXTGuideVC];
        [[XTGuidePagesViewController shareXTGuideVC] initWithXTGuideView:images];
        [XTGuidePagesViewController shareXTGuideVC].delegate = self;
    }else{
        [self click];
    }
- (void)click
{
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    [self.window.layer transitionWithAnimType:TransitionAnimTypeRamdom subType:TransitionSubtypesFromRamdom curve:TransitionCurveRamdom duration:2.0f];
}

还会持续更新