第一次运行一个程序时候,我们会看到新手引导页,就是程序引导图,除非后来版本升级,或者卸载软件后重装才会再次看到引导页
我们先来看效果:
![](https://img.haomeiwen.com/i2406962/318c410796f55733.gif)
这里我第一次运行程序时候,直接是有引导图的,后来两次运行都没有再次出现引导图。除非下次版本更新之后,或者卸载再次运行程序才会出现引导图。
先给大家两张图片分析一下
![](https://img.haomeiwen.com/i2406962/ae81214115c37dd9.png)
![](https://img.haomeiwen.com/i2406962/21d72aec04a7d968.png)
现在来教一下大家如何用代码实现引导图
第一步:在AppDelegate.m文件中
![](https://img.haomeiwen.com/i2406962/5bf3f5b856a25302.png)
代码呈上
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 1.创建窗口
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
// 2.设置根控制器
NSString *key = @"CFBundleVersion";
// 上一次的使用版本(存储在沙盒中的版本号)
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
// 当前软件的版本号(从Info.plist中获得)
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
if ([currentVersion isEqualToString:lastVersion]) { // 版本号相同:这次打开和上次打开的是同一个版本
self.window.rootViewController = [[LYWTabBarViewController alloc] init];
} else { // 这次打开的版本和上一次不一样,显示新特性
self.window.rootViewController = [[LYWNewfeatureViewController alloc] init];
// 将当前的版本号存进沙盒
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// 3.显示窗口
[self.window makeKeyAndVisible];
return YES;
}
第二步:在LYWNewfeatureViewController.m 中实现简单的轮播图效果
#import "LYWNewfeatureViewController.h"
#import "LYWTabBarViewController.h"
#define NewfeatureCount 3
@interface LYWNewfeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIPageControl *pageControl;
@end
@implementation LYWNewfeatureViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScrollView];
}
//创建UIScrollView并添加图片
- (void)setupScrollView
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = [UIScreen mainScreen].bounds;
[self.view addSubview:scrollView];
scrollView.bounces = NO;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(3*kScreenWidth, 0);
scrollView.delegate = self;
for (NSInteger i = 0; i < NewfeatureCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.x = i * kScreenWidth;
imageView.y = 0;
imageView.width = kScreenWidth;
imageView.height = kScreenHeight;
NSString *name = [NSString stringWithFormat:@"f%ld-5",i+1];
imageView.image = [UIImage imageNamed:name];
[scrollView addSubview:imageView];
if (i == NewfeatureCount - 1) {
[self setupStartBtn:imageView];
}
}
// 4.添加pageControl:分页,展示目前看的是第几页
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.numberOfPages = NewfeatureCount;
pageControl.backgroundColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
pageControl.pageIndicatorTintColor = KRGB(189, 189, 189);
pageControl.centerX = kScreenWidth * 0.5;
pageControl.centerY = kScreenHeight - 50;
[self.view addSubview:pageControl];
self.pageControl = pageControl;
}
//左上角的灰色跳过按钮
-(void)createSkipBt
{
UIButton *skipBt = [UIButton buttonWithType:UIButtonTypeCustom];
skipBt.frame = CGRectMake(kScreenWidth - 90, 40, 80, 30);
skipBt.backgroundColor = [UIColor colorWithRed:0.3 green:0.3f blue:0.3f alpha:0.3];
[skipBt setTitle:@"跳过" forState:UIControlStateNormal];
[skipBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
skipBt.layer.cornerRadius = 10;
skipBt.clipsToBounds = YES;
skipBt.tag = 10;
[skipBt addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:skipBt];
}
//手动拖拽结束时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
double page = scrollView.contentOffset.x / scrollView.width;
// 四舍五入计算出页码
self.pageControl.currentPage = (int)(page + 0.5);
// 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
// 1.5四舍五入 1.5 + 0.5 = 2.0 强转为整数(int)2.0= 2
// 1.6四舍五入 1.6 + 0.5 = 2.1 强转为整数(int)2.1= 2
// 0.7四舍五入 0.7 + 0.5 = 1.2 强转为整数(int)1.2= 1
}
//给最后一张图片添加 进入问医生按钮
- (void)setupStartBtn:(UIImageView *)imgView
{
imgView.userInteractionEnabled = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"cancel_ask"] forState:UIControlStateNormal];
btn.size = btn.currentBackgroundImage.size;
btn.centerX = imgView.width * 0.5;
btn.centerY = imgView.height * 0.75;
[btn setTitle:@"进入问医生" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:btn];
}
//进入问医生按钮点击事件
-(void)BtnDidClicked
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = [[LYWTabBarViewController alloc] init];
}
@end
这样就👌了,其实很简单,不过是最近一个外包项目过来,然后我接手了,给它做一了一个启动引导图,闲来没事,就写写这片文章打法自己无聊的时间,也方便很多初学者学习
备注:
如果有不足或者错误的地方还望各位读者批评指正,可以评论留言,笔者收到后第一时间回复。
QQ/微信:2366889552 /lan2018yingwei。
简书号:凡尘一笑:[简书]
http://www.jianshu.com/users/0158007b8d17/latest_articles
感谢各位观众老爷的阅读,如果觉得笔者写的还凑合,可以关注或收藏一下,不定期分享一些好玩的实用的demo给大家。
文/凡尘一笑(简书作者)
原文链接: http://www.jianshu.com/p/8ae080edb3ea
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
网友评论