第一次運(yùn)行一個(gè)程序時(shí)候低匙,我們會(huì)看到新手引導(dǎo)頁朋凉,就是程序引導(dǎo)圖,除非后來版本升級给梅,或者卸載軟件后重裝才會(huì)再次看到引導(dǎo)頁
我們先來看效果:
這里我第一次運(yùn)行程序時(shí)候,直接是有引導(dǎo)圖的双揪,后來兩次運(yùn)行都沒有再次出現(xiàn)引導(dǎo)圖动羽。除非下次版本更新之后,或者卸載再次運(yùn)行程序才會(huì)出現(xiàn)引導(dǎo)圖盟榴。
先給大家兩張圖片分析一下
現(xiàn)在來教一下大家如何用代碼實(shí)現(xiàn)引導(dǎo)圖
第一步:在AppDelegate.m文件中
代碼呈上
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 1.創(chuàng)建窗口
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
// 2.設(shè)置根控制器
NSString *key = @"CFBundleVersion";
// 上一次的使用版本(存儲(chǔ)在沙盒中的版本號)
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
// 當(dāng)前軟件的版本號(從Info.plist中獲得)
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
if ([currentVersion isEqualToString:lastVersion]) { // 版本號相同:這次打開和上次打開的是同一個(gè)版本
self.window.rootViewController = [[LYWTabBarViewController alloc] init];
} else { // 這次打開的版本和上一次不一樣曹质,顯示新特性
self.window.rootViewController = [[LYWNewfeatureViewController alloc] init];
// 將當(dāng)前的版本號存進(jìn)沙盒
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// 3.顯示窗口
[self.window makeKeyAndVisible];
return YES;
}
第二步:在LYWNewfeatureViewController.m 中實(shí)現(xiàn)簡單的輪播圖效果
#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];
}
//創(chuàng)建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];
}
//手動(dòng)拖拽結(jié)束時(shí)候調(diào)用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
double page = scrollView.contentOffset.x / scrollView.width;
// 四舍五入計(jì)算出頁碼
self.pageControl.currentPage = (int)(page + 0.5);
// 1.3四舍五入 1.3 + 0.5 = 1.8 強(qiáng)轉(zhuǎn)為整數(shù)(int)1.8= 1
// 1.5四舍五入 1.5 + 0.5 = 2.0 強(qiáng)轉(zhuǎn)為整數(shù)(int)2.0= 2
// 1.6四舍五入 1.6 + 0.5 = 2.1 強(qiáng)轉(zhuǎn)為整數(shù)(int)2.1= 2
// 0.7四舍五入 0.7 + 0.5 = 1.2 強(qiáng)轉(zhuǎn)為整數(shù)(int)1.2= 1
}
//給最后一張圖片添加 進(jìn)入問醫(yī)生按鈕
- (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:@"進(jìn)入問醫(yī)生" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(BtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
[imgView addSubview:btn];
}
//進(jìn)入問醫(yī)生按鈕點(diǎn)擊事件
-(void)BtnDidClicked
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = [[LYWTabBarViewController alloc] init];
}
@end
這樣就??了擎场,其實(shí)很簡單羽德,不過是最近一個(gè)外包項(xiàng)目過來,然后我接手了迅办,給它做一了一個(gè)啟動(dòng)引導(dǎo)圖宅静,閑來沒事,就寫寫這片文章打法自己無聊的時(shí)間站欺,也方便很多初學(xué)者學(xué)習(xí)
備注:
如果有不足或者錯(cuò)誤的地方還望各位讀者批評指正姨夹,可以評論留言,筆者收到后第一時(shí)間回復(fù)矾策。
QQ/微信:2366889552 /lan2018yingwei磷账。
簡書號:凡塵一笑:[簡書]
http://www.reibang.com/users/0158007b8d17/latest_articles
感謝各位觀眾老爺?shù)拈喿x,如果覺得筆者寫的還湊合贾虽,可以關(guān)注或收藏一下逃糟,不定期分享一些好玩的實(shí)用的demo給大家。
文/凡塵一笑(簡書作者)
原文鏈接: http://www.reibang.com/p/8ae080edb3ea
著作權(quán)歸作者所有蓬豁,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)绰咽,并標(biāo)注“簡書作者”。