iOS 一步一步带你实现引导页

标签: ios引导页oc
3307人阅读 评论(2) 收藏 举报
分类:

这里写图片描述
直接上代码(简简单单)
创建一个控制器

.h
@protocol selectDelegate <NSObject>
- (void)click;
@end
@interface XTGuidePagesViewController : UIViewController
@property (nonatomic, strong) UIButton *btnEnter;
// 初始化引导页
- (void)initWithXTGuideView:(NSArray *)images;
// 版本信息判断
- (BOOL)isShow;
@property (nonatomic, assign) id<selectDelegate> 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];

    // pageControl
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, s_w / 2, 30)];
    self.pageControl.center = CGPointMake(s_w / 2, s_h - 95);
    [self.view addSubview:self.pageControl];
    self.pageControl.numberOfPages = images.count;
}

单利

+ (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];
}
#pragma mark - ScrollerView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / s_w;
}

在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];
}

还会持续更新, 喜欢的请关注

2
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:223413次
    • 积分:3724
    • 等级:
    • 排名:第9558名
    • 原创:133篇
    • 转载:9篇
    • 译文:0篇
    • 评论:46条
    我的微博
    公众号
      我的微信公众号
      为你推荐最新的博文~更有惊喜等着你
    博客专栏