前言: 在第一次安装App, 或者更新App的时候, 常常会出现一个引导界面, 昨天晚上花了点时间, 写了个引导页,太晚就没发出来, 实现一个引导页其实并不困难. 请看代码
点我下载Demo 有封装好的工具类 有用请Star Thanks
效果图
引导页实际是一个单一的控制器 --UI 布局
- (void)guidePageControllerWithImages:(NSArray *)images
{
UIScrollView *gui = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, s_w, s_h)];
gui.delegate = self;
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];
if (i == images.count - 1) {
[btn setTitle:@"点击进入" forState:UIControlStateNormal];
}
btn.frame = CGRectMake(s_w * i, s_h - 50, 100, 30);
btn.center = CGPointMake(s_w / 2, s_h - 60);
btn.layer.cornerRadius = 4;
btn.clipsToBounds = YES;
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;
}
GNU C详细请看参考链接 这是参考资料 首页不要拒绝我
跳过&点击进入按钮实现方法
- (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"];
if (localVersion == nil || ![currentVersion isEqualToString:localVersion]) {
[XTGuidePagesViewController 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];
}
pageControl跟随移动
#pragma mark - ScrollerView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.pageControl.currentPage = scrollView.contentOffset.x / s_w;
}
如何使用呢?
// 测试的时候改变info 里的版本号就可以了
NSArray *images = @[@"1", @"2", @"3", @"4"];
BOOL y = [XTGuidePagesViewController isShow];
if (y) {
XTGuidePagesViewController *xt = [[XTGuidePagesViewController alloc] init];
self.window.rootViewController = xt;
xt.delegate = self;
[xt guidePageControllerWithImages:images];
}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];
}
篇幅太长就不贴出动画代码了, 转场动画如何书写请看Demo
您可能还对这些感兴趣, 欢迎关注我顺便点个赞, 一起学习, 您还可以查看我的主页, 加入我创建的交流群.
全面的Swift学习资料整理 -- 简书
Swift版仿简书App淘宝App很友好弹出view效果 -- 简书
---------------------------------------
走心文章, 值得点赞 ---文/夏天然后
微博-点我@夏天是个大人了 || QQQ: 498143780
网友评论